<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>05_selenium_rc</title>
<link rel="stylesheet" type="text/css" href="css/mystyle.css"/>
</head>
<body>

<div class="body">
            
  <div class="section" id="selenium-1-selenium-rc">
<h1>Selenium 1 (Selenium RC)<a class="headerlink" href="#selenium-1-selenium-rc" title="Permalink to this headline">¶</a></h1>
<div class="section" id="introduction">
<span id="chapter05-reference"></span><h2>Introduction<a class="headerlink" href="#introduction" title="Permalink to this headline">¶</a></h2>
<p>As you can read in <a class="reference internal" href="01_introducing_selenium.jsp#selenium-history"><span class="std std-ref">Brief History of The Selenium Project</span></a>,
Selenium RC was the main Selenium project for a long time, before the
WebDriver/Selenium merge brought up Selenium 2, the newest and more powerful
tool.</p>
<p>Selenium 1 is still actively supported (mostly in maintenance mode) and
provides some features that may not be available in Selenium 2 for a while,
including support for several languages (Java, Javascript, Ruby, PHP, Python,
Perl and C#) and support for almost every browser out there.</p>
</div>
<div class="section" id="how-selenium-rc-works">
<h2>How Selenium RC Works<a class="headerlink" href="#how-selenium-rc-works" title="Permalink to this headline">¶</a></h2>
<p>First, we will describe how the components of Selenium RC operate and the role each plays in running
your test scripts.</p>
<div class="section" id="rc-components">
<h3>RC Components<a class="headerlink" href="#rc-components" title="Permalink to this headline">¶</a></h3>
<p>Selenium RC components are:</p>
<ul class="simple">
<li>The Selenium Server which launches and kills browsers, interprets and runs the Selenese commands passed from the test program, and acts as an <em>HTTP proxy</em>, intercepting and verifying HTTP messages passed between the browser and the AUT.</li>
<li>Client libraries which provide the interface between each programming language and the Selenium RC Server.</li>
</ul>
<p>Here is a simplified architecture diagram….</p>
<img alt="_images/chapt5_img01_Architecture_Diagram_Simple.png" class="align-center" src="_images/chapt5_img01_Architecture_Diagram_Simple.png">
<p>The diagram shows the client libraries communicate with the
Server passing each Selenium command for execution. Then the server passes the
Selenium command to the browser using Selenium-Core JavaScript commands.  The
browser, using its JavaScript interpreter, executes the Selenium command.  This
runs the Selenese action or verification you specified in your test script.</p>
</div>
<div class="section" id="selenium-server">
<h3>Selenium Server<a class="headerlink" href="#selenium-server" title="Permalink to this headline">¶</a></h3>
<p>Selenium Server receives Selenium commands from your test program,
interprets them, and reports back to your program the results of
running those tests.</p>
<p>The RC server bundles Selenium Core and  automatically injects
it into the browser.  This occurs when your test program opens the
browser (using a client library API function).
Selenium-Core is a JavaScript program, actually a set of JavaScript
functions which interprets and executes Selenese commands using the
browser’s built-in JavaScript interpreter.</p>
<p>The Server receives the Selenese commands from your test program
using simple HTTP GET/POST requests. This means you can use any
programming language that can send HTTP requests to automate
Selenium tests on the browser.</p>
</div>
<div class="section" id="client-libraries">
<h3>Client Libraries<a class="headerlink" href="#client-libraries" title="Permalink to this headline">¶</a></h3>
<p>The client libraries provide the programming support that allows you to
run Selenium commands from a program of your own design.  There is a
different client library for each supported language.  A Selenium client
library provides a programming interface (API), i.e., a set of functions,
which run Selenium commands from your own program. Within each interface,
there is a programming function that supports each Selenese command.</p>
<p>The client library takes a Selenese command and passes it to the Selenium Server
for processing a specific action or test against the application under test
(AUT).  The client library
also receives the result of that command and passes it back to your program.
Your program can receive the result and store it into a program variable and
report it as a success or failure,
or possibly take corrective action if it was an unexpected error.</p>
<p>So to create a test program, you simply write a program that runs
a set of Selenium commands using a client library API.  And, optionally, if
you already have a Selenese test script created in the Selenium-IDE, you can
<em>generate the Selenium RC code</em>. The Selenium-IDE can translate (using its
Export menu item) its Selenium commands into a client-driver’s API function
calls.  See the Selenium-IDE chapter for specifics on exporting RC code from
Selenium-IDE.</p>
</div>
</div>
<div class="section" id="installation">
<h2>Installation<a class="headerlink" href="#installation" title="Permalink to this headline">¶</a></h2>
<p>Installation is rather a misnomer for Selenium. Selenium has a set of libraries available
in the programming language of your choice. You could download them from <a class="reference external" href="http://seleniumhq.org/download/">downloads page</a></p>
<p>Once you’ve chosen a language to work with, you simply need to:</p>
<ul class="simple">
<li>Install the Selenium RC Server.</li>
<li>Set up a programming project using a language specific client driver.</li>
</ul>
<div class="section" id="installing-selenium-server">
<h3>Installing Selenium Server<a class="headerlink" href="#installing-selenium-server" title="Permalink to this headline">¶</a></h3>
<p>The Selenium RC server is simply a Java <em>jar</em> file (<em>selenium-server-standalone-&lt;version-number&gt;.jar</em>), which doesn’t
require any special installation. Just downloading the zip file and extracting the
server in the desired directory is sufficient.</p>
</div>
<div class="section" id="running-selenium-server">
<h3>Running Selenium Server<a class="headerlink" href="#running-selenium-server" title="Permalink to this headline">¶</a></h3>
<p>Before starting any tests you must start the server.  Go to the directory
where Selenium RC’s server is located and run the following from a command-line
console.</p>
<blockquote>
<div>java -jar selenium-server-standalone-&lt;version-number&gt;.jar</div></blockquote>
<p>This can be simplified by creating
a batch or shell executable file (.bat on Windows and .sh on Linux) containing the command
above. Then make a shortcut to that executable on your
desktop and simply double-click the icon to start the server.</p>
<p>For the server to run you’ll need Java installed
and the PATH environment variable correctly configured to run it from the console.
You can check that you have Java correctly installed by running the following
on a console.</p>
<blockquote>
<div>java -version</div></blockquote>
<p>If you get a version number (which needs to be 1.5 or later), you’re ready to start using Selenium RC.</p>
</div>
<div class="section" id="using-the-java-client-driver">
<h3>Using the Java Client Driver<a class="headerlink" href="#using-the-java-client-driver" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>Download Selenium java client driver zip from the SeleniumHQ <a class="reference external" href="http://seleniumhq.org/download/">downloads page</a>.</li>
<li>Extract selenium-java-&lt;version-number&gt;.jar file</li>
<li>Open your desired Java IDE (Eclipse, NetBeans, IntelliJ, Netweaver, etc.)</li>
<li>Create a java project.</li>
<li>Add the selenium-java-&lt;version-number&gt;.jar files to your project as references.</li>
<li>Add to your project classpath the file selenium-java-&lt;version-number&gt;.jar.</li>
<li>From Selenium-IDE, export a script to a Java file and include it in your Java
project, or write your Selenium test in Java using the selenium-java-client API.
The API is presented later in this chapter.  You can either use JUnit, or TestNg
to run your test, or you can write your own simple main() program.  These concepts are
explained later in this section.</li>
<li>Run Selenium server from the console.</li>
<li>Execute your test from the Java IDE or from the command-line.</li>
</ul>
<p>For details on Java test project configuration, see the Appendix sections
<span class="xref std std-ref">Configuring Selenium RC With Eclipse</span>
and
<span class="xref std std-ref">Configuring Selenium RC With Intellij</span>.</p>
</div>
<div class="section" id="using-the-python-client-driver">
<h3>Using the Python Client Driver<a class="headerlink" href="#using-the-python-client-driver" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>Install Selenium via PIP, instructions linked at SeleniumHQ <a class="reference external" href="http://seleniumhq.org/download/">downloads page</a></li>
<li>Either write your Selenium test in Python or export
a script from Selenium-IDE to a python file.</li>
<li>Run Selenium server from the console</li>
<li>Execute your test from a console or your Python IDE</li>
</ul>
<p>For details on Python client driver configuration, see the appendix
<span class="xref std std-ref">Python Client Driver Configuration</span>.</p>
</div>
<div class="section" id="using-the-net-client-driver">
<h3>Using the .NET Client Driver<a class="headerlink" href="#using-the-net-client-driver" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>Download Selenium RC from the SeleniumHQ <a class="reference external" href="http://seleniumhq.org/download/">downloads page</a></li>
<li>Extract the folder</li>
<li>Download and install <a class="reference external" href="http://www.nunit.org/index.php?p=download">NUnit</a> (
Note:  You can use NUnit as your test engine.  If you’re not familiar yet with
NUnit, you can also write a simple main() function to run your tests;
however NUnit is very useful as a test engine.)</li>
<li>Open your desired .Net IDE (Visual Studio, SharpDevelop, MonoDevelop)</li>
<li>Create a class library (.dll)</li>
<li>Add references to the following DLLs: nmock.dll, nunit.core.dll, nunit.
framework.dll, ThoughtWorks.Selenium.Core.dll, ThoughtWorks.Selenium.IntegrationTests.dll
and ThoughtWorks.Selenium.UnitTests.dll</li>
<li>Write your Selenium test in a .Net language (C#, VB.Net), or export
a script from Selenium-IDE to a C# file and copy this code into the class file
you just created.</li>
<li>Write your own simple main() program or you can include NUnit in your project
for running your test.  These concepts are explained later in this chapter.</li>
<li>Run Selenium server from console</li>
<li>Run your test either from the IDE, from the NUnit GUI or from the command line</li>
</ul>
<p>For specific details on .NET client driver configuration with Visual Studio, see the appendix
<a class="reference internal" href="appendix_installing_dotnet_driver_client.jsp#configuring-selenium-rc-net-reference"><span class="std std-ref">.NET client driver configuration</span></a>.</p>
</div>
<div class="section" id="using-the-ruby-client-driver">
<h3>Using the Ruby Client Driver<a class="headerlink" href="#using-the-ruby-client-driver" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>If you do not already have RubyGems, install it from <a class="reference external" href="http://docs.rubygems.org/read/chapter/3">RubyForge</a></li>
<li>Run <code class="docutils literal"><span class="pre">gem</span> <span class="pre">install</span> <span class="pre">selenium-client</span></code></li>
<li>At the top of your test script, add <code class="docutils literal"><span class="pre">require</span> <span class="pre">"selenium/client"</span></code></li>
<li>Write your test script using any Ruby test harness (eg Test::Unit,
Mini::Test or RSpec).</li>
<li>Run Selenium RC server from the console.</li>
<li>Execute your test in the same way you would run any other Ruby
script.</li>
</ul>
<p>For details on Ruby client driver configuration, see the <a class="reference external" href="http://selenium-client.rubyforge.org/">Selenium-Client documentation</a></p>
</div>
</div>
<div class="section" id="from-selenese-to-a-program">
<h2>From Selenese to a Program<a class="headerlink" href="#from-selenese-to-a-program" title="Permalink to this headline">¶</a></h2>
<p>The primary task for using Selenium RC is to convert your Selenese into a programming
language.  In this section, we provide several different
language-specific examples.</p>
<div class="section" id="sample-test-script">
<h3>Sample Test Script<a class="headerlink" href="#sample-test-script" title="Permalink to this headline">¶</a></h3>
<p>Let’s start with an example Selenese test script.  Imagine recording
the following test with Selenium-IDE.</p>
<table border="1" class="docutils" id="google-search-example">
<colgroup>
<col width="32%">
<col width="47%">
<col width="21%">
</colgroup>
<tbody valign="top">
<tr class="row-odd"><td>open</td>
<td>/</td>
<td>&nbsp;</td>
</tr>
<tr class="row-even"><td>type</td>
<td>q</td>
<td>selenium rc</td>
</tr>
<tr class="row-odd"><td>clickAndWait</td>
<td>btnG</td>
<td>&nbsp;</td>
</tr>
<tr class="row-even"><td>assertTextPresent</td>
<td>Results * for selenium rc</td>
<td>&nbsp;</td>
</tr>
</tbody>
</table>
<p>Note: This example would work with the Google search page <a class="reference external" href="http://www.google.com">http://www.google.com</a></p>
</div>
<div class="section" id="selenese-as-programming-code">
<h3>Selenese as Programming Code<a class="headerlink" href="#selenese-as-programming-code" title="Permalink to this headline">¶</a></h3>
<p>Here is the test script exported (via Selenium-IDE) to each of the supported
programming languages.  If you have at least basic knowledge of an object-
oriented programming language, you will understand how Selenium
runs Selenese commands by reading one of these
examples.  To see an example in a specific language, select one of these buttons.</p>
<input type="image" class="toggler toggler-csharp" value="csharp" src="images/icons/csharp.png" style="padding: 3px;"><div class="highlight-csharp" style="display: none;"><div class="highlight"><pre><span></span><span class="k">using</span> <span class="nn">System</span><span class="p">;</span>
<span class="k">using</span> <span class="nn">System.Text</span><span class="p">;</span>
<span class="k">using</span> <span class="nn">System.Text.RegularExpressions</span><span class="p">;</span>
<span class="k">using</span> <span class="nn">System.Threading</span><span class="p">;</span>
<span class="k">using</span> <span class="nn">NUnit.Framework</span><span class="p">;</span>
<span class="k">using</span> <span class="nn">Selenium</span><span class="p">;</span>

<span class="k">namespace</span> <span class="nn">SeleniumTests</span>
<span class="p">{</span>
<span class="na">    [TestFixture]</span>
    <span class="k">public</span> <span class="k">class</span> <span class="nc">NewTest</span>
    <span class="p">{</span>
        <span class="k">private</span> <span class="n">ISelenium</span> <span class="n">selenium</span><span class="p">;</span>
        <span class="k">private</span> <span class="n">StringBuilder</span> <span class="n">verificationErrors</span><span class="p">;</span>

<span class="na">        [SetUp]</span>
        <span class="k">public</span> <span class="k">void</span> <span class="nf">SetupTest</span><span class="p">()</span>
        <span class="p">{</span>
            <span class="n">selenium</span> <span class="p">=</span> <span class="k">new</span> <span class="n">DefaultSelenium</span><span class="p">(</span><span class="s">"localhost"</span><span class="p">,</span> <span class="m">4444</span><span class="p">,</span> <span class="s">"*firefox"</span><span class="p">,</span> <span class="s">"http://www.google.com/"</span><span class="p">);</span>
            <span class="n">selenium</span><span class="p">.</span><span class="n">Start</span><span class="p">();</span>
            <span class="n">verificationErrors</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StringBuilder</span><span class="p">();</span>
        <span class="p">}</span>

<span class="na">        [TearDown]</span>
        <span class="k">public</span> <span class="k">void</span> <span class="nf">TeardownTest</span><span class="p">()</span>
        <span class="p">{</span>
            <span class="k">try</span>
            <span class="p">{</span>
                <span class="n">selenium</span><span class="p">.</span><span class="n">Stop</span><span class="p">();</span>
            <span class="p">}</span>
            <span class="k">catch</span> <span class="p">(</span><span class="n">Exception</span><span class="p">)</span>
            <span class="p">{</span>
                <span class="c1">// Ignore errors if unable to close the browser</span>
            <span class="p">}</span>
            <span class="n">Assert</span><span class="p">.</span><span class="n">AreEqual</span><span class="p">(</span><span class="s">""</span><span class="p">,</span> <span class="n">verificationErrors</span><span class="p">.</span><span class="n">ToString</span><span class="p">());</span>
        <span class="p">}</span>

<span class="na">        [Test]</span>
        <span class="k">public</span> <span class="k">void</span> <span class="nf">TheNewTest</span><span class="p">()</span>
        <span class="p">{</span>
            <span class="n">selenium</span><span class="p">.</span><span class="n">Open</span><span class="p">(</span><span class="s">"/"</span><span class="p">);</span>
            <span class="n">selenium</span><span class="p">.</span><span class="n">Type</span><span class="p">(</span><span class="s">"q"</span><span class="p">,</span> <span class="s">"selenium rc"</span><span class="p">);</span>
            <span class="n">selenium</span><span class="p">.</span><span class="n">Click</span><span class="p">(</span><span class="s">"btnG"</span><span class="p">);</span>
            <span class="n">selenium</span><span class="p">.</span><span class="n">WaitForPageToLoad</span><span class="p">(</span><span class="s">"30000"</span><span class="p">);</span>
            <span class="n">Assert</span><span class="p">.</span><span class="n">AreEqual</span><span class="p">(</span><span class="s">"selenium rc - Google Search"</span><span class="p">,</span> <span class="n">selenium</span><span class="p">.</span><span class="n">GetTitle</span><span class="p">());</span>
        <span class="p">}</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span>    <span class="cm">/** Add JUnit framework to your classpath if not already there</span>
<span class="cm">     *  for this example to work</span>
<span class="cm">    */</span>
<span class="kn">package</span> <span class="nn">com.example.tests</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">com.thoughtworks.selenium.*</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">java.util.regex.Pattern</span><span class="o">;</span>

<span class="kd">public</span> <span class="kd">class</span> <span class="nc">NewTest</span> <span class="kd">extends</span> <span class="n">SeleneseTestCase</span> <span class="o">{</span>
    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">setUp</span><span class="o">()</span> <span class="kd">throws</span> <span class="n">Exception</span> <span class="o">{</span>
        <span class="n">setUp</span><span class="o">(</span><span class="s">"http://www.google.com/"</span><span class="o">,</span> <span class="s">"*firefox"</span><span class="o">);</span>
    <span class="o">}</span>
      <span class="kd">public</span> <span class="kt">void</span> <span class="nf">testNew</span><span class="o">()</span> <span class="kd">throws</span> <span class="n">Exception</span> <span class="o">{</span>
          <span class="n">selenium</span><span class="o">.</span><span class="na">open</span><span class="o">(</span><span class="s">"/"</span><span class="o">);</span>
          <span class="n">selenium</span><span class="o">.</span><span class="na">type</span><span class="o">(</span><span class="s">"q"</span><span class="o">,</span> <span class="s">"selenium rc"</span><span class="o">);</span>
          <span class="n">selenium</span><span class="o">.</span><span class="na">click</span><span class="o">(</span><span class="s">"btnG"</span><span class="o">);</span>
          <span class="n">selenium</span><span class="o">.</span><span class="na">waitForPageToLoad</span><span class="o">(</span><span class="s">"30000"</span><span class="o">);</span>
          <span class="n">assertTrue</span><span class="o">(</span><span class="n">selenium</span><span class="o">.</span><span class="na">isTextPresent</span><span class="o">(</span><span class="s">"Results * for selenium rc"</span><span class="o">));</span>
    <span class="o">}</span>
<span class="o">}</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-perl" value="perl" src="images/icons/perl.png" style="padding: 3px;"><div class="highlight-perl" style="display: none;"><div class="highlight"><pre><span></span><span class="k">use</span> <span class="nn">strict</span><span class="p">;</span>
<span class="k">use</span> <span class="nn">warnings</span><span class="p">;</span>
<span class="k">use</span> <span class="nn">Time::HiRes</span> <span class="sx">qw(sleep)</span><span class="p">;</span>
<span class="k">use</span> <span class="nn">Test::WWW::Selenium</span><span class="p">;</span>
<span class="k">use</span> <span class="nn">Test::More</span> <span class="s">"no_plan"</span><span class="p">;</span>
<span class="k">use</span> <span class="nn">Test::Exception</span><span class="p">;</span>

<span class="k">my</span> <span class="nv">$sel</span> <span class="o">=</span> <span class="nn">Test::WWW::Selenium</span><span class="o">-&gt;</span><span class="k">new</span><span class="p">(</span> <span class="n">host</span> <span class="o">=&gt;</span> <span class="s">"localhost"</span><span class="p">,</span>
    <span class="n">port</span> <span class="o">=&gt;</span> <span class="mi">4444</span><span class="p">,</span>
    <span class="n">browser</span> <span class="o">=&gt;</span> <span class="s">"*firefox"</span><span class="p">,</span>
    <span class="n">browser_url</span> <span class="o">=&gt;</span> <span class="s">"http://www.google.com/"</span> <span class="p">);</span>

<span class="nv">$sel</span><span class="o">-&gt;</span><span class="n">open_ok</span><span class="p">(</span><span class="s">"/"</span><span class="p">);</span>
<span class="nv">$sel</span><span class="o">-&gt;</span><span class="n">is_element_present_ok</span><span class="p">(</span><span class="s">"q"</span><span class="p">);</span>
<span class="nv">$sel</span><span class="o">-&gt;</span><span class="n">type_ok</span><span class="p">(</span><span class="s">"q"</span><span class="p">,</span> <span class="s">"selenium rc"</span><span class="p">);</span>
<span class="nv">$sel</span><span class="o">-&gt;</span><span class="n">value_is</span><span class="p">(</span><span class="s">"q"</span><span class="p">,</span> <span class="s">"selenium rc"</span><span class="p">);</span>
<span class="nv">$sel</span><span class="o">-&gt;</span><span class="n">is_element_present_ok</span><span class="p">(</span><span class="s">"btnG"</span><span class="p">);</span>
<span class="nv">$sel</span><span class="o">-&gt;</span><span class="n">click_ok</span><span class="p">(</span><span class="s">"btnG"</span><span class="p">);</span>
<span class="nv">$sel</span><span class="o">-&gt;</span><span class="n">wait_for_text_present</span><span class="p">(</span><span class="s">"results"</span><span class="p">,</span> <span class="mi">30000</span><span class="p">);</span>
<span class="nv">$sel</span><span class="o">-&gt;</span><span class="n">is_element_present_ok</span><span class="p">(</span><span class="s">"resultStats"</span><span class="p">);</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-php" value="php" src="images/icons/php.png" style="padding: 3px;"><div class="highlight-php" style="display: none;"><div class="highlight"><pre><span></span><span class="cp">&lt;?php</span>

<span class="k">require_once</span> <span class="s1">'PHPUnit/Extensions/SeleniumTestCase.php'</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">Example</span> <span class="k">extends</span> <span class="nx">PHPUnit_Extensions_SeleniumTestCase</span>
<span class="p">{</span>
  <span class="k">function</span> <span class="nf">setUp</span><span class="p">()</span>
  <span class="p">{</span>
    <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">setBrowser</span><span class="p">(</span><span class="s2">"*firefox"</span><span class="p">);</span>
    <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">setBrowserUrl</span><span class="p">(</span><span class="s2">"http://www.google.com/"</span><span class="p">);</span>
  <span class="p">}</span>

  <span class="k">function</span> <span class="nf">testMyTestCase</span><span class="p">()</span>
  <span class="p">{</span>
    <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">open</span><span class="p">(</span><span class="s2">"/"</span><span class="p">);</span>
    <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">type</span><span class="p">(</span><span class="s2">"q"</span><span class="p">,</span> <span class="s2">"selenium rc"</span><span class="p">);</span>
    <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">click</span><span class="p">(</span><span class="s2">"btnG"</span><span class="p">);</span>
    <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">waitForPageToLoad</span><span class="p">(</span><span class="s2">"30000"</span><span class="p">);</span>
    <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">assertTrue</span><span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">isTextPresent</span><span class="p">(</span><span class="s2">"Results * for selenium rc"</span><span class="p">));</span>
  <span class="p">}</span>
<span class="p">}</span>
<span class="cp">?&gt;</span><span class="x"></span>
</pre></div>
</div>
<input type="image" class="toggler toggler-python" value="python" src="images/icons/python.png" style="padding: 3px;"><div class="highlight-python" style="display: none;"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">selenium</span> <span class="kn">import</span> <span class="n">selenium</span>
<span class="kn">import</span> <span class="nn">unittest</span><span class="o">,</span> <span class="nn">time</span><span class="o">,</span> <span class="nn">re</span>

<span class="k">class</span> <span class="nc">NewTest</span><span class="p">(</span><span class="n">unittest</span><span class="o">.</span><span class="n">TestCase</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">setUp</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">verificationErrors</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">selenium</span> <span class="o">=</span> <span class="n">selenium</span><span class="p">(</span><span class="s2">"localhost"</span><span class="p">,</span> <span class="mi">4444</span><span class="p">,</span> <span class="s2">"*firefox"</span><span class="p">,</span>
                <span class="s2">"http://www.google.com/"</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">selenium</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">test_new</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">sel</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">selenium</span>
        <span class="n">sel</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s2">"/"</span><span class="p">)</span>
        <span class="n">sel</span><span class="o">.</span><span class="n">type</span><span class="p">(</span><span class="s2">"q"</span><span class="p">,</span> <span class="s2">"selenium rc"</span><span class="p">)</span>
        <span class="n">sel</span><span class="o">.</span><span class="n">click</span><span class="p">(</span><span class="s2">"btnG"</span><span class="p">)</span>
        <span class="n">sel</span><span class="o">.</span><span class="n">wait_for_page_to_load</span><span class="p">(</span><span class="s2">"30000"</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">failUnless</span><span class="p">(</span><span class="n">sel</span><span class="o">.</span><span class="n">is_text_present</span><span class="p">(</span><span class="s2">"Results * for selenium rc"</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">tearDown</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">selenium</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">([],</span> <span class="bp">self</span><span class="o">.</span><span class="n">verificationErrors</span><span class="p">)</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-ruby" value="ruby" src="images/icons/ruby.png" style="padding: 3px;"><div class="highlight-ruby" style="display: none;"><div class="highlight"><pre><span></span><span class="nb">require</span> <span class="s2">"selenium/client"</span>
<span class="nb">require</span> <span class="s2">"test/unit"</span>

<span class="k">class</span> <span class="nc">NewTest</span> <span class="o">&lt;</span> <span class="no">Test</span><span class="o">::</span><span class="no">Unit</span><span class="o">::</span><span class="no">TestCase</span>
  <span class="k">def</span> <span class="nf">setup</span>
    <span class="vi">@verification_errors</span> <span class="o">=</span> <span class="o">[]</span>
    <span class="k">if</span> <span class="vg">$selenium</span>
      <span class="vi">@selenium</span> <span class="o">=</span> <span class="vg">$selenium</span>
    <span class="k">else</span>
      <span class="vi">@selenium</span> <span class="o">=</span>  <span class="no">Selenium</span><span class="o">::</span><span class="no">Client</span><span class="o">::</span><span class="no">Driver</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="s2">"localhost"</span><span class="p">,</span> <span class="mi">4444</span><span class="p">,</span> <span class="s2">"*firefox"</span><span class="p">,</span> <span class="s2">"http://www.google.com/"</span><span class="p">,</span> <span class="mi">60</span><span class="p">);</span>
      <span class="vi">@selenium</span><span class="o">.</span><span class="n">start</span>
    <span class="k">end</span>
    <span class="vi">@selenium</span><span class="o">.</span><span class="n">set_context</span><span class="p">(</span><span class="s2">"test_new"</span><span class="p">)</span>
  <span class="k">end</span>

  <span class="k">def</span> <span class="nf">teardown</span>
    <span class="vi">@selenium</span><span class="o">.</span><span class="n">stop</span> <span class="k">unless</span> <span class="vg">$selenium</span>
    <span class="n">assert_equal</span> <span class="o">[]</span><span class="p">,</span> <span class="vi">@verification_errors</span>
  <span class="k">end</span>

  <span class="k">def</span> <span class="nf">test_new</span>
    <span class="vi">@selenium</span><span class="o">.</span><span class="n">open</span> <span class="s2">"/"</span>
    <span class="vi">@selenium</span><span class="o">.</span><span class="n">type</span> <span class="s2">"q"</span><span class="p">,</span> <span class="s2">"selenium rc"</span>
    <span class="vi">@selenium</span><span class="o">.</span><span class="n">click</span> <span class="s2">"btnG"</span>
    <span class="vi">@selenium</span><span class="o">.</span><span class="n">wait_for_page_to_load</span> <span class="s2">"30000"</span>
    <span class="n">assert</span> <span class="vi">@selenium</span><span class="o">.</span><span class="n">is_text_present</span><span class="p">(</span><span class="s2">"Results * for selenium rc"</span><span class="p">)</span>
  <span class="k">end</span>
<span class="k">end</span>
</pre></div>
</div>
<p>In the next section we’ll explain how to build a test program using the generated code.</p>
</div>
</div>
<div class="section" id="programming-your-test">
<h2>Programming Your Test<a class="headerlink" href="#programming-your-test" title="Permalink to this headline">¶</a></h2>
<p>Now we’ll illustrate how to program your own tests using examples in each of the
supported programming languages.
There are essentially two tasks:</p>
<ul class="simple">
<li>Generate your script into a programming
language from Selenium-IDE, optionally modifying the result.</li>
<li>Write a very simple main program that executes the generated code.</li>
</ul>
<p>Optionally, you can adopt a test engine platform like JUnit or TestNG for Java,
or NUnit for .NET if you are using one of those languages.</p>
<p>Here, we show language-specific examples.  The language-specific APIs tend to
differ from one to another, so you’ll find a separate explanation for each.</p>
<ul class="simple">
<li><a class="reference internal" href="#java">Java</a></li>
<li><a class="reference internal" href="#c">C#</a></li>
<li><a class="reference internal" href="#python">Python</a></li>
<li><a class="reference internal" href="#ruby">Ruby</a></li>
<li><a class="reference internal" href="#perl-php">Perl, PHP</a></li>
</ul>
<div class="section" id="java">
<h3>Java<a class="headerlink" href="#java" title="Permalink to this headline">¶</a></h3>
<p>For Java, people use either JUnit or TestNG as the test engine.
Some development environments like Eclipse have direct support for these via
plug-ins.  This makes it even easier. Teaching JUnit or TestNG is beyond the scope of
this document however materials may be found online and there are publications
available.  If you are already a “java-shop” chances are your developers will
already have some experience with one of these test frameworks.</p>
<p>You will probably want to rename the test class from “NewTest” to something
of your own choosing.  Also, you will need to change the browser-open
parameters in the statement:</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span><span class="nv">selenium</span> <span class="o">=</span> new DefaultSelenium<span class="o">(</span><span class="s2">"localhost"</span>, <span class="m">4444</span>, <span class="s2">"*iehta"</span>, <span class="s2">"http://www.google.com/"</span><span class="o">)</span><span class="p">;</span>
</pre></div>
</div>
<p>The Selenium-IDE generated code will look like this.  This example
has comments added manually for additional clarity.</p>
<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="kn">package</span> <span class="nn">com.example.tests</span><span class="o">;</span>
<span class="c1">// We specify the package of our tests</span>

<span class="kn">import</span> <span class="nn">com.thoughtworks.selenium.*</span><span class="o">;</span>
<span class="c1">// This is the driver's import. You'll use this for instantiating a</span>
<span class="c1">// browser and making it do what you need.</span>

<span class="kn">import</span> <span class="nn">java.util.regex.Pattern</span><span class="o">;</span>
<span class="c1">// Selenium-IDE add the Pattern module because it's sometimes used for</span>
<span class="c1">// regex validations. You can remove the module if it's not used in your</span>
<span class="c1">// script.</span>

<span class="kd">public</span> <span class="kd">class</span> <span class="nc">NewTest</span> <span class="kd">extends</span> <span class="n">SeleneseTestCase</span> <span class="o">{</span>
<span class="c1">// We create our Selenium test case</span>

      <span class="kd">public</span> <span class="kt">void</span> <span class="nf">setUp</span><span class="o">()</span> <span class="kd">throws</span> <span class="n">Exception</span> <span class="o">{</span>
        <span class="n">setUp</span><span class="o">(</span><span class="s">"http://www.google.com/"</span><span class="o">,</span> <span class="s">"*firefox"</span><span class="o">);</span>
             <span class="c1">// We instantiate and start the browser</span>
      <span class="o">}</span>

      <span class="kd">public</span> <span class="kt">void</span> <span class="nf">testNew</span><span class="o">()</span> <span class="kd">throws</span> <span class="n">Exception</span> <span class="o">{</span>
           <span class="n">selenium</span><span class="o">.</span><span class="na">open</span><span class="o">(</span><span class="s">"/"</span><span class="o">);</span>
           <span class="n">selenium</span><span class="o">.</span><span class="na">type</span><span class="o">(</span><span class="s">"q"</span><span class="o">,</span> <span class="s">"selenium rc"</span><span class="o">);</span>
           <span class="n">selenium</span><span class="o">.</span><span class="na">click</span><span class="o">(</span><span class="s">"btnG"</span><span class="o">);</span>
           <span class="n">selenium</span><span class="o">.</span><span class="na">waitForPageToLoad</span><span class="o">(</span><span class="s">"30000"</span><span class="o">);</span>
           <span class="n">assertTrue</span><span class="o">(</span><span class="n">selenium</span><span class="o">.</span><span class="na">isTextPresent</span><span class="o">(</span><span class="s">"Results * for selenium rc"</span><span class="o">));</span>
           <span class="c1">// These are the real test steps</span>
     <span class="o">}</span>
<span class="o">}</span>
</pre></div>
</div>
</div>
<div class="section" id="c">
<h3>C#<a class="headerlink" href="#c" title="Permalink to this headline">¶</a></h3>
<p>The .NET Client Driver works with Microsoft.NET.
It can be used with any .NET testing framework
like NUnit or the Visual Studio 2005 Team System.</p>
<p>Selenium-IDE assumes you will use NUnit as your testing framework.
You can see this in the generated code below.  It includes the <em>using</em> statement
for NUnit along with corresponding NUnit attributes identifying
the role for each member function of the test class.</p>
<p>You will probably have to rename the test class from “NewTest” to
something of your own choosing.  Also, you will need to change the browser-open
parameters in the statement:</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span><span class="nv">selenium</span> <span class="o">=</span> new DefaultSelenium<span class="o">(</span><span class="s2">"localhost"</span>, <span class="m">4444</span>, <span class="s2">"*iehta"</span>, <span class="s2">"http://www.google.com/"</span><span class="o">)</span><span class="p">;</span>
</pre></div>
</div>
<p>The generated code will look similar to this.</p>
<input type="image" class="toggler toggler-csharp" value="csharp" src="images/icons/csharp.png" style="padding: 3px;"><div class="highlight-csharp" style="display: none;"><div class="highlight"><pre><span></span><span class="k">using</span> <span class="nn">System</span><span class="p">;</span>
<span class="k">using</span> <span class="nn">System.Text</span><span class="p">;</span>
<span class="k">using</span> <span class="nn">System.Text.RegularExpressions</span><span class="p">;</span>
<span class="k">using</span> <span class="nn">System.Threading</span><span class="p">;</span>
<span class="k">using</span> <span class="nn">NUnit.Framework</span><span class="p">;</span>
<span class="k">using</span> <span class="nn">Selenium</span><span class="p">;</span>

<span class="k">namespace</span> <span class="nn">SeleniumTests</span>

<span class="p">{</span>
<span class="na">    [TestFixture]</span>

    <span class="k">public</span> <span class="k">class</span> <span class="nc">NewTest</span>

    <span class="p">{</span>
    <span class="k">private</span> <span class="n">ISelenium</span> <span class="n">selenium</span><span class="p">;</span>

    <span class="k">private</span> <span class="n">StringBuilder</span> <span class="n">verificationErrors</span><span class="p">;</span>

<span class="na">    [SetUp]</span>

    <span class="k">public</span> <span class="k">void</span> <span class="nf">SetupTest</span><span class="p">()</span>

    <span class="p">{</span>
        <span class="n">selenium</span> <span class="p">=</span> <span class="k">new</span> <span class="n">DefaultSelenium</span><span class="p">(</span><span class="s">"localhost"</span><span class="p">,</span> <span class="m">4444</span><span class="p">,</span> <span class="s">"*iehta"</span><span class="p">,</span>
        <span class="s">"http://www.google.com/"</span><span class="p">);</span>

        <span class="n">selenium</span><span class="p">.</span><span class="n">Start</span><span class="p">();</span>

        <span class="n">verificationErrors</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StringBuilder</span><span class="p">();</span>
    <span class="p">}</span>

<span class="na">    [TearDown]</span>

    <span class="k">public</span> <span class="k">void</span> <span class="nf">TeardownTest</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="k">try</span>
        <span class="p">{</span>
        <span class="n">selenium</span><span class="p">.</span><span class="n">Stop</span><span class="p">();</span>
        <span class="p">}</span>

        <span class="k">catch</span> <span class="p">(</span><span class="n">Exception</span><span class="p">)</span>
        <span class="p">{</span>
        <span class="c1">// Ignore errors if unable to close the browser</span>
        <span class="p">}</span>

        <span class="n">Assert</span><span class="p">.</span><span class="n">AreEqual</span><span class="p">(</span><span class="s">""</span><span class="p">,</span> <span class="n">verificationErrors</span><span class="p">.</span><span class="n">ToString</span><span class="p">());</span>
    <span class="p">}</span>
<span class="na">    [Test]</span>

    <span class="k">public</span> <span class="k">void</span> <span class="nf">TheNewTest</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="c1">// Open Google search engine.</span>
        <span class="n">selenium</span><span class="p">.</span><span class="n">Open</span><span class="p">(</span><span class="s">"http://www.google.com/"</span><span class="p">);</span>

        <span class="c1">// Assert Title of page.</span>
        <span class="n">Assert</span><span class="p">.</span><span class="n">AreEqual</span><span class="p">(</span><span class="s">"Google"</span><span class="p">,</span> <span class="n">selenium</span><span class="p">.</span><span class="n">GetTitle</span><span class="p">());</span>

        <span class="c1">// Provide search term as "Selenium OpenQA"</span>
        <span class="n">selenium</span><span class="p">.</span><span class="n">Type</span><span class="p">(</span><span class="s">"q"</span><span class="p">,</span> <span class="s">"Selenium OpenQA"</span><span class="p">);</span>

        <span class="c1">// Read the keyed search term and assert it.</span>
        <span class="n">Assert</span><span class="p">.</span><span class="n">AreEqual</span><span class="p">(</span><span class="s">"Selenium OpenQA"</span><span class="p">,</span> <span class="n">selenium</span><span class="p">.</span><span class="n">GetValue</span><span class="p">(</span><span class="s">"q"</span><span class="p">));</span>

        <span class="c1">// Click on Search button.</span>
        <span class="n">selenium</span><span class="p">.</span><span class="n">Click</span><span class="p">(</span><span class="s">"btnG"</span><span class="p">);</span>

        <span class="c1">// Wait for page to load.</span>
        <span class="n">selenium</span><span class="p">.</span><span class="n">WaitForPageToLoad</span><span class="p">(</span><span class="s">"5000"</span><span class="p">);</span>

        <span class="c1">// Assert that "www.openqa.org" is available in search results.</span>
        <span class="n">Assert</span><span class="p">.</span><span class="n">IsTrue</span><span class="p">(</span><span class="n">selenium</span><span class="p">.</span><span class="n">IsTextPresent</span><span class="p">(</span><span class="s">"www.openqa.org"</span><span class="p">));</span>

        <span class="c1">// Assert that page title is - "Selenium OpenQA - Google Search"</span>
        <span class="n">Assert</span><span class="p">.</span><span class="n">AreEqual</span><span class="p">(</span><span class="s">"Selenium OpenQA - Google Search"</span><span class="p">,</span>
                     <span class="n">selenium</span><span class="p">.</span><span class="n">GetTitle</span><span class="p">());</span>
    <span class="p">}</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>You can allow NUnit to manage the execution
of your tests.  Or alternatively, you can write a simple main() program that
instantiates the test object and runs each of the three methods, SetupTest(),
TheNewTest(), and TeardownTest() in turn.</p>
</div>
<div class="section" id="python">
<h3>Python<a class="headerlink" href="#python" title="Permalink to this headline">¶</a></h3>
<p>Pyunit is the test framework to use for Python. To learn Pyunit refer
to its <cite>official documentation</cite> &lt;<a class="reference external" href="http://docs.python.org/library/unittest.html">http://docs.python.org/library/unittest.html</a>&gt;_.</p>
<p>The basic test structure is:</p>
<input type="image" class="toggler toggler-python" value="python" src="images/icons/python.png" style="padding: 3px;"><div class="highlight-python" style="display: none;"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">selenium</span> <span class="kn">import</span> <span class="n">selenium</span>
<span class="c1"># This is the driver's import.  You'll use this class for instantiating a</span>
<span class="c1"># browser and making it do what you need.</span>

<span class="kn">import</span> <span class="nn">unittest</span><span class="o">,</span> <span class="nn">time</span><span class="o">,</span> <span class="nn">re</span>
<span class="c1"># This are the basic imports added by Selenium-IDE by default.</span>
<span class="c1"># You can remove the modules if they are not used in your script.</span>

<span class="k">class</span> <span class="nc">NewTest</span><span class="p">(</span><span class="n">unittest</span><span class="o">.</span><span class="n">TestCase</span><span class="p">):</span>
<span class="c1"># We create our unittest test case</span>

    <span class="k">def</span> <span class="nf">setUp</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">verificationErrors</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="c1"># This is an empty array where we will store any verification errors</span>
        <span class="c1"># we find in our tests</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">selenium</span> <span class="o">=</span> <span class="n">selenium</span><span class="p">(</span><span class="s2">"localhost"</span><span class="p">,</span> <span class="mi">4444</span><span class="p">,</span> <span class="s2">"*firefox"</span><span class="p">,</span>
                <span class="s2">"http://www.google.com/"</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">selenium</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
        <span class="c1"># We instantiate and start the browser</span>

    <span class="k">def</span> <span class="nf">test_new</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># This is the test code.  Here you should put the actions you need</span>
        <span class="c1"># the browser to do during your test.</span>

        <span class="n">sel</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">selenium</span>
        <span class="c1"># We assign the browser to the variable "sel" (just to save us from</span>
        <span class="c1"># typing "self.selenium" each time we want to call the browser).</span>

        <span class="n">sel</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s2">"/"</span><span class="p">)</span>
        <span class="n">sel</span><span class="o">.</span><span class="n">type</span><span class="p">(</span><span class="s2">"q"</span><span class="p">,</span> <span class="s2">"selenium rc"</span><span class="p">)</span>
        <span class="n">sel</span><span class="o">.</span><span class="n">click</span><span class="p">(</span><span class="s2">"btnG"</span><span class="p">)</span>
        <span class="n">sel</span><span class="o">.</span><span class="n">wait_for_page_to_load</span><span class="p">(</span><span class="s2">"30000"</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">failUnless</span><span class="p">(</span><span class="n">sel</span><span class="o">.</span><span class="n">is_text_present</span><span class="p">(</span><span class="s2">"Results * for selenium rc"</span><span class="p">))</span>
        <span class="c1"># These are the real test steps</span>

    <span class="k">def</span> <span class="nf">tearDown</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">selenium</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span>
        <span class="c1"># we close the browser (I'd recommend you to comment this line while</span>
        <span class="c1"># you are creating and debugging your tests)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">([],</span> <span class="bp">self</span><span class="o">.</span><span class="n">verificationErrors</span><span class="p">)</span>
        <span class="c1"># And make the test fail if we found that any verification errors</span>
        <span class="c1"># were found</span>
</pre></div>
</div>
</div>
<div class="section" id="ruby">
<h3>Ruby<a class="headerlink" href="#ruby" title="Permalink to this headline">¶</a></h3>
<p>Old (pre 2.0) versions of Selenium-IDE generate Ruby code that requires the old Selenium
gem. Therefore, it is advisable to update any Ruby scripts generated by the
IDE as follows:</p>
<p>1. On line 1, change <code class="docutils literal"><span class="pre">require</span> <span class="pre">"selenium"</span></code> to <code class="docutils literal"><span class="pre">require</span>
<span class="pre">"selenium/client"</span></code></p>
<p>2. On line 11, change <code class="docutils literal"><span class="pre">Selenium::SeleniumDriver.new</span></code> to
<code class="docutils literal"><span class="pre">Selenium::Client::Driver.new</span></code></p>
<p>You probably also want to change the class name to something more
informative than “Untitled,” and change the test method’s name to
something other than “test_untitled.”</p>
<p>Here is a simple example created by modifying the Ruby code generated
by Selenium IDE, as described above.</p>
<input type="image" class="toggler toggler-ruby" value="ruby" src="images/icons/ruby.png" style="padding: 3px;"><div class="highlight-ruby" style="display: none;"><div class="highlight"><pre><span></span><span class="c1"># load the Selenium-Client gem</span>
<span class="nb">require</span> <span class="s2">"selenium/client"</span>

<span class="c1"># Load Test::Unit, Ruby's default test framework.</span>
<span class="c1"># If you prefer RSpec, see the examples in the Selenium-Client</span>
<span class="c1"># documentation.</span>
<span class="nb">require</span> <span class="s2">"test/unit"</span>

<span class="k">class</span> <span class="nc">Untitled</span> <span class="o">&lt;</span> <span class="no">Test</span><span class="o">::</span><span class="no">Unit</span><span class="o">::</span><span class="no">TestCase</span>

  <span class="c1"># The setup method is called before each test.</span>
  <span class="k">def</span> <span class="nf">setup</span>

    <span class="c1"># This array is used to capture errors and display them at the</span>
    <span class="c1"># end of the test run.</span>
    <span class="vi">@verification_errors</span> <span class="o">=</span> <span class="o">[]</span>

    <span class="c1"># Create a new instance of the Selenium-Client driver.</span>
    <span class="vi">@selenium</span> <span class="o">=</span> <span class="no">Selenium</span><span class="o">::</span><span class="no">Client</span><span class="o">::</span><span class="no">Driver</span><span class="o">.</span><span class="n">new</span> <span class="p">\</span>
      <span class="ss">:host</span> <span class="o">=&gt;</span> <span class="s2">"localhost"</span><span class="p">,</span>
      <span class="ss">:port</span> <span class="o">=&gt;</span> <span class="mi">4444</span><span class="p">,</span>
      <span class="ss">:browser</span> <span class="o">=&gt;</span> <span class="s2">"*chrome"</span><span class="p">,</span>
      <span class="ss">:url</span> <span class="o">=&gt;</span> <span class="s2">"http://www.google.com/"</span><span class="p">,</span>
      <span class="ss">:timeout_in_second</span> <span class="o">=&gt;</span> <span class="mi">60</span>

    <span class="c1"># Start the browser session</span>
    <span class="vi">@selenium</span><span class="o">.</span><span class="n">start</span>

    <span class="c1"># Print a message in the browser-side log and status bar</span>
    <span class="c1"># (optional).</span>
    <span class="vi">@selenium</span><span class="o">.</span><span class="n">set_context</span><span class="p">(</span><span class="s2">"test_untitled"</span><span class="p">)</span>

  <span class="k">end</span>

  <span class="c1"># The teardown method is called after each test.</span>
  <span class="k">def</span> <span class="nf">teardown</span>

    <span class="c1"># Stop the browser session.</span>
    <span class="vi">@selenium</span><span class="o">.</span><span class="n">stop</span>

    <span class="c1"># Print the array of error messages, if any.</span>
    <span class="n">assert_equal</span> <span class="o">[]</span><span class="p">,</span> <span class="vi">@verification_errors</span>
  <span class="k">end</span>

  <span class="c1"># This is the main body of your test.</span>
  <span class="k">def</span> <span class="nf">test_untitled</span>

    <span class="c1"># Open the root of the site we specified when we created the</span>
    <span class="c1"># new driver instance, above.</span>
    <span class="vi">@selenium</span><span class="o">.</span><span class="n">open</span> <span class="s2">"/"</span>

    <span class="c1"># Type 'selenium rc' into the field named 'q'</span>
    <span class="vi">@selenium</span><span class="o">.</span><span class="n">type</span> <span class="s2">"q"</span><span class="p">,</span> <span class="s2">"selenium rc"</span>

    <span class="c1"># Click the button named "btnG"</span>
    <span class="vi">@selenium</span><span class="o">.</span><span class="n">click</span> <span class="s2">"btnG"</span>

    <span class="c1"># Wait for the search results page to load.</span>
    <span class="c1"># Note that we don't need to set a timeout here, because that</span>
    <span class="c1"># was specified when we created the new driver instance, above.</span>
    <span class="vi">@selenium</span><span class="o">.</span><span class="n">wait_for_page_to_load</span>

    <span class="k">begin</span>

       <span class="c1"># Test whether the search results contain the expected text.</span>
       <span class="c1"># Notice that the star (*) is a wildcard that matches any</span>
       <span class="c1"># number of characters.</span>
       <span class="n">assert</span> <span class="vi">@selenium</span><span class="o">.</span><span class="n">is_text_present</span><span class="p">(</span><span class="s2">"Results * for selenium rc"</span><span class="p">)</span>

    <span class="k">rescue</span> <span class="no">Test</span><span class="o">::</span><span class="no">Unit</span><span class="o">::</span><span class="no">AssertionFailedError</span>

       <span class="c1"># If the assertion fails, push it onto the array of errors.</span>
       <span class="vi">@verification_errors</span> <span class="o">&lt;&lt;</span> <span class="vg">$!</span>

    <span class="k">end</span>
  <span class="k">end</span>
<span class="k">end</span>
</pre></div>
</div>
</div>
<div class="section" id="perl-php">
<h3>Perl, PHP<a class="headerlink" href="#perl-php" title="Permalink to this headline">¶</a></h3>
<p>The members of the documentation team
have not used Selenium RC with Perl or PHP.  If you are using Selenium RC with either of
these two languages please contact the Documentation Team (see the chapter on contributing).
We would love to include some examples from you and your experiences, to support Perl and PHP users.</p>
</div>
</div>
<div class="section" id="learning-the-api">
<h2>Learning the API<a class="headerlink" href="#learning-the-api" title="Permalink to this headline">¶</a></h2>
<p>The Selenium RC API uses naming conventions
that, assuming you understand Selenese, much of the interface
will be self-explanatory. Here, however, we explain the most critical and
possibly less obvious aspects.</p>
<div class="section" id="starting-the-browser">
<h3>Starting the Browser<a class="headerlink" href="#starting-the-browser" title="Permalink to this headline">¶</a></h3>
<input type="image" class="toggler toggler-csharp" value="csharp" src="images/icons/csharp.png" style="padding: 3px;"><div class="highlight-csharp" style="display: none;"><div class="highlight"><pre><span></span><span class="n">selenium</span> <span class="p">=</span> <span class="k">new</span> <span class="n">DefaultSelenium</span><span class="p">(</span><span class="s">"localhost"</span><span class="p">,</span> <span class="m">4444</span><span class="p">,</span> <span class="s">"*firefox"</span><span class="p">,</span> <span class="s">"http://www.google.com/"</span><span class="p">);</span>
<span class="n">selenium</span><span class="p">.</span><span class="n">Start</span><span class="p">();</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">setUp</span><span class="o">(</span><span class="s">"http://www.google.com/"</span><span class="o">,</span> <span class="s">"*firefox"</span><span class="o">);</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-perl" value="perl" src="images/icons/perl.png" style="padding: 3px;"><div class="highlight-perl" style="display: none;"><div class="highlight"><pre><span></span><span class="k">my</span> <span class="nv">$sel</span> <span class="o">=</span> <span class="nn">Test::WWW::Selenium</span><span class="o">-&gt;</span><span class="k">new</span><span class="p">(</span> <span class="n">host</span> <span class="o">=&gt;</span> <span class="s">"localhost"</span><span class="p">,</span>
                                    <span class="n">port</span> <span class="o">=&gt;</span> <span class="mi">4444</span><span class="p">,</span>
                                    <span class="n">browser</span> <span class="o">=&gt;</span> <span class="s">"*firefox"</span><span class="p">,</span>
                                    <span class="n">browser_url</span> <span class="o">=&gt;</span> <span class="s">"http://www.google.com/"</span> <span class="p">);</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-php" value="php" src="images/icons/php.png" style="padding: 3px;"><div class="highlight-php" style="display: none;"><div class="highlight"><pre><span></span><span class="x">$this-&gt;setBrowser("*firefox");</span>
<span class="x">$this-&gt;setBrowserUrl("http://www.google.com/");</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-python" value="python" src="images/icons/python.png" style="padding: 3px;"><div class="highlight-python" style="display: none;"><div class="highlight"><pre><span></span><span class="bp">self</span><span class="o">.</span><span class="n">selenium</span> <span class="o">=</span> <span class="n">selenium</span><span class="p">(</span><span class="s2">"localhost"</span><span class="p">,</span> <span class="mi">4444</span><span class="p">,</span> <span class="s2">"*firefox"</span><span class="p">,</span>
                         <span class="s2">"http://www.google.com/"</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">selenium</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-ruby" value="ruby" src="images/icons/ruby.png" style="padding: 3px;"><div class="highlight-ruby" style="display: none;"><div class="highlight"><pre><span></span><span class="vi">@selenium</span> <span class="o">=</span> <span class="no">Selenium</span><span class="o">::</span><span class="no">ClientDriver</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="s2">"localhost"</span><span class="p">,</span> <span class="mi">4444</span><span class="p">,</span> <span class="s2">"*firefox"</span><span class="p">,</span> <span class="s2">"http://www.google.com/"</span><span class="p">,</span> <span class="mi">10000</span><span class="p">);</span>
<span class="vi">@selenium</span><span class="o">.</span><span class="n">start</span>
</pre></div>
</div>
<p>Each of these examples opens the browser and represents that browser
by assigning a “browser instance” to a program variable.  This
program variable is then used to call methods from the browser.
These methods execute the Selenium commands, i.e. like <em>open</em> or <em>type</em> or the <em>verify</em>
commands.</p>
<p>The parameters required when creating the browser instance
are:</p>
<dl class="docutils">
<dt><strong>host</strong></dt>
<dd>Specifies the IP address of the computer where the server is located. Usually, this is
the same machine as where the client is running, so in this case <em>localhost</em> is passed.  In some clients this is an optional parameter.</dd>
<dt><strong>port</strong></dt>
<dd>Specifies the TCP/IP socket where the server is listening waiting
for the client to establish a connection.  This also is optional in some
client drivers.</dd>
<dt><strong>browser</strong></dt>
<dd>The browser in which you want to run the tests. This is a required
parameter.</dd>
<dt><strong>url</strong></dt>
<dd>The base url of the application under test. This is required by all the
client libs and is integral information for starting up the browser-proxy-AUT communication.</dd>
</dl>
<p>Note that some of the client libraries require the browser to be started explicitly by calling
its <em>start()</em> method.</p>
</div>
<div class="section" id="running-commands">
<h3>Running Commands<a class="headerlink" href="#running-commands" title="Permalink to this headline">¶</a></h3>
<p>Once you have the browser initialized and assigned to a variable (generally
named “selenium”) you can make it run Selenese commands by calling the respective
methods from the browser variable. For example, to call the <em>type</em> method
of the selenium object:</p>
<blockquote>
<div>selenium.type(“field-id”,”string to type”)</div></blockquote>
<p>In the background the browser will actually perform a <em>type</em> operation,
essentially identical to a user typing input into the browser, by
using the locator and the string you specified during the method call.</p>
</div>
</div>
<div class="section" id="reporting-results">
<h2>Reporting Results<a class="headerlink" href="#reporting-results" title="Permalink to this headline">¶</a></h2>
<p>Selenium RC does not have its own mechanism for reporting results.  Rather, it allows
you to build your reporting customized to your needs using features of your
chosen programming language.  That’s great, but what if you simply want something
quick that’s already done for you?  Often an existing library or test framework can
meet your needs faster than developing your own test reporting code.</p>
<div class="section" id="test-framework-reporting-tools">
<h3>Test Framework Reporting Tools<a class="headerlink" href="#test-framework-reporting-tools" title="Permalink to this headline">¶</a></h3>
<p>Test frameworks are available for many programming languages.  These, along with
their primary function of providing a flexible test engine for executing your tests,
include library code for reporting results.  For example, Java has two
commonly used test frameworks, JUnit and TestNG.  .NET also has its own, NUnit.</p>
<p>We won’t teach the frameworks themselves here; that’s beyond the scope of this
user guide.  We will simply introduce the framework features that relate to Selenium
along with some techniques you can apply.  There are good books available on these
test frameworks however along with information on the internet.</p>
</div>
<div class="section" id="test-report-libraries">
<h3>Test Report Libraries<a class="headerlink" href="#test-report-libraries" title="Permalink to this headline">¶</a></h3>
<p>Also available are third-party libraries specifically created for reporting
test results in your chosen programming language.  These often support a
variety of formats such as HTML or PDF.</p>
</div>
<div class="section" id="what-s-the-best-approach">
<h3>What’s The Best Approach?<a class="headerlink" href="#what-s-the-best-approach" title="Permalink to this headline">¶</a></h3>
<p>Most people new to the testing frameworks will begin with the framework’s
built-in reporting features.  From there most will examine any available libraries
as that’s less time consuming than developing your own.  As you begin to use
Selenium no doubt you will start putting in your own “print statements” for
reporting progress.  That may gradually lead to you developing your own
reporting, possibly in parallel to using a library or test framework.  Regardless,
after the initial, but short, learning curve you will naturally develop what works
best for your own situation.</p>
</div>
<div class="section" id="test-reporting-examples">
<h3>Test Reporting Examples<a class="headerlink" href="#test-reporting-examples" title="Permalink to this headline">¶</a></h3>
<p>To illustrate, we’ll direct you to some specific tools in some of the other languages
supported by Selenium.  The ones listed here are commonly used and have been used
extensively (and therefore recommended) by the authors of this guide.</p>
<div class="section" id="test-reports-in-java">
<h4>Test Reports in Java<a class="headerlink" href="#test-reports-in-java" title="Permalink to this headline">¶</a></h4>
<ul class="simple">
<li>If Selenium Test cases are developed using JUnit then JUnit Report can be used
to generate test reports. Refer to <a class="reference external" href="http://ant.apache.org/manual/Tasks/junitreport.html">JUnit Report</a> for specifics.</li>
</ul>
<ul class="simple">
<li>If Selenium Test cases are developed using TestNG then no external task
is required to generate test reports. The TestNG framework generates an
HTML report which list details of tests. See <a class="reference external" href="http://testng.org/doc/documentation-main.html#test-results">TestNG Report</a> for more.</li>
</ul>
<ul class="simple">
<li>ReportNG is a HTML reporting plug-in for the TestNG framework.
It is intended as a replacement for the default TestNG HTML report.
ReportNG provides a simple, colour-coded view of the test results.
See <a class="reference external" href="http://reportng.uncommons.org/">ReportNG</a> for more.</li>
</ul>
<ul>
<li><p class="first">Also, for a very nice summary report try using TestNG-xslt.
A TestNG-xslt Report looks like this.</p>
<img alt="_images/chapt5_TestNGxsltReport.png" src="_images/chapt5_TestNGxsltReport.png">
<p>See <a class="reference external" href="http://code.google.com/p/testng-xslt/">TestNG-xslt</a> for more.</p>
</li>
</ul>
<p><strong>Logging the Selenese Commands</strong></p>
<ul class="simple">
<li>Logging Selenium can be used to generate a report of all the Selenese commands
in your test along with the success or failure of each. Logging Selenium extends
the Java client driver to add this Selenese logging ability. Please refer
to <a class="reference external" href="http://loggingselenium.sourceforge.net/index.html">Logging Selenium</a>.</li>
</ul>
</div>
<div class="section" id="test-reports-for-python">
<h4>Test Reports for Python<a class="headerlink" href="#test-reports-for-python" title="Permalink to this headline">¶</a></h4>
<ul class="simple">
<li>When using Python Client Driver then HTMLTestRunner can be used to
generate a Test Report. See <a class="reference external" href="http://tungwaiyip.info/software/HTMLTestRunner.html">HTMLTestRunner</a>.</li>
</ul>
</div>
<div class="section" id="test-reports-for-ruby">
<h4>Test Reports for Ruby<a class="headerlink" href="#test-reports-for-ruby" title="Permalink to this headline">¶</a></h4>
<ul class="simple">
<li>If RSpec framework is used for writing Selenium Test Cases in Ruby
then its HTML report can be used to generate a test report.
Refer to <a class="reference external" href="http://rspec.info/documentation/tools/rake.html">RSpec Report</a> for more.</li>
</ul>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">If you are interested in a language independent log of what’s going
on, take a look at <a class="reference internal" href="#selenium-server-logging">Selenium Server Logging</a></p>
</div>
</div>
</div>
</div>
<div class="section" id="adding-some-spice-to-your-tests">
<h2>Adding Some Spice to Your Tests<a class="headerlink" href="#adding-some-spice-to-your-tests" title="Permalink to this headline">¶</a></h2>
<p>Now we’ll get to the whole reason for using Selenium RC, adding programming logic to your tests.
It’s the same as for any program.  Program flow is controlled using condition statements
and iteration.  In addition you can report progress information using I/O.  In this section
we’ll show some examples of how programming language constructs can be combined with
Selenium to solve common testing problems.</p>
<p>You will find as you transition from the simple tests of the existence of
page elements to tests of dynamic functionality involving multiple web-pages and
varying data that you will require programming logic for verifying expected
results.  Basically, the Selenium-IDE does not support iteration and
standard condition statements.  You can do some conditions by embedding javascript
in Selenese parameters, however
iteration is impossible, and most conditions will be much easier in a
programming language.  In addition, you may need exception handling for
error recovery.  For these reasons and others, we have written this section
to illustrate the use of common programming techniques to
give you greater ‘verification power’ in your automated testing.</p>
<p>The examples in this section are written
in C# and Java, although the code is simple and can be easily adapted to the other supported
languages.  If you have some basic knowledge
of an object-oriented programming language you shouldn’t have difficulty understanding this section.</p>
<div class="section" id="iteration">
<h3>Iteration<a class="headerlink" href="#iteration" title="Permalink to this headline">¶</a></h3>
<p>Iteration is one of the most common things people need to do in their tests.
For example, you may want to to execute a search multiple times.  Or, perhaps for
verifying your test results you need to process a “result set” returned from a database.</p>
<p>Using the same <a class="reference internal" href="#google-search-example">Google search example</a> we used earlier, let’s
check the Selenium search results. This test could use the Selenese:</p>
<table border="1" class="docutils">
<colgroup>
<col width="30%">
<col width="47%">
<col width="23%">
</colgroup>
<tbody valign="top">
<tr class="row-odd"><td>open</td>
<td>/</td>
<td>&nbsp;</td>
</tr>
<tr class="row-even"><td>type</td>
<td>q</td>
<td>selenium rc</td>
</tr>
<tr class="row-odd"><td>clickAndWait</td>
<td>btnG</td>
<td>&nbsp;</td>
</tr>
<tr class="row-even"><td>assertTextPresent</td>
<td>Results * for selenium rc</td>
<td>&nbsp;</td>
</tr>
<tr class="row-odd"><td>type</td>
<td>q</td>
<td>selenium ide</td>
</tr>
<tr class="row-even"><td>clickAndWait</td>
<td>btnG</td>
<td>&nbsp;</td>
</tr>
<tr class="row-odd"><td>assertTextPresent</td>
<td>Results * for selenium ide</td>
<td>&nbsp;</td>
</tr>
<tr class="row-even"><td>type</td>
<td>q</td>
<td>selenium grid</td>
</tr>
<tr class="row-odd"><td>clickAndWait</td>
<td>btnG</td>
<td>&nbsp;</td>
</tr>
<tr class="row-even"><td>assertTextPresent</td>
<td>Results * for selenium grid</td>
<td>&nbsp;</td>
</tr>
</tbody>
</table>
<p>The code has been repeated to run the same steps 3 times.  But multiple
copies of the same code is not good program practice because it’s more
work to maintain.  By using a programming language, we can iterate
over the search results for a more flexible and maintainable solution.</p>
<p><strong>In C#:</strong></p>
<div class="highlight-c#"><div class="highlight"><pre><span></span><span class="c1">// Collection of String values.</span>
<span class="n">String</span><span class="p">[]</span> <span class="n">arr</span> <span class="p">=</span> <span class="p">{</span><span class="s">"ide"</span><span class="p">,</span> <span class="s">"rc"</span><span class="p">,</span> <span class="s">"grid"</span><span class="p">};</span>

<span class="c1">// Execute loop for each String in array 'arr'.</span>
<span class="k">foreach</span> <span class="p">(</span><span class="n">String</span> <span class="n">s</span> <span class="k">in</span> <span class="n">arr</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">sel</span><span class="p">.</span><span class="n">open</span><span class="p">(</span><span class="s">"/"</span><span class="p">);</span>
    <span class="n">sel</span><span class="p">.</span><span class="n">type</span><span class="p">(</span><span class="s">"q"</span><span class="p">,</span> <span class="s">"selenium "</span> <span class="p">+</span><span class="n">s</span><span class="p">);</span>
    <span class="n">sel</span><span class="p">.</span><span class="n">click</span><span class="p">(</span><span class="s">"btnG"</span><span class="p">);</span>
    <span class="n">sel</span><span class="p">.</span><span class="n">waitForPageToLoad</span><span class="p">(</span><span class="s">"30000"</span><span class="p">);</span>
    <span class="n">assertTrue</span><span class="p">(</span><span class="s">"Expected text: "</span> <span class="p">+</span><span class="n">s</span><span class="p">+</span> <span class="s">" is missing on page."</span>
    <span class="p">,</span> <span class="n">sel</span><span class="p">.</span><span class="n">isTextPresent</span><span class="p">(</span><span class="s">"Results * for selenium "</span> <span class="p">+</span> <span class="n">s</span><span class="p">));</span>
 <span class="p">}</span>
</pre></div>
</div>
</div>
<div class="section" id="condition-statements">
<h3>Condition Statements<a class="headerlink" href="#condition-statements" title="Permalink to this headline">¶</a></h3>
<p>To illustrate using conditions in tests we’ll start with an example.
A common problem encountered while running Selenium tests occurs when an
expected element is not available on page.  For example, when running the
following line:</p>
<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">selenium</span><span class="o">.</span><span class="na">type</span><span class="o">(</span><span class="s">"q"</span><span class="o">,</span> <span class="s">"selenium "</span> <span class="o">+</span><span class="n">s</span><span class="o">);</span>
</pre></div>
</div>
<p>If element ‘q’ is not on the page then an exception is
thrown:</p>
<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">com</span><span class="o">.</span><span class="na">thoughtworks</span><span class="o">.</span><span class="na">selenium</span><span class="o">.</span><span class="na">SeleniumException</span><span class="o">:</span> <span class="n">ERROR</span><span class="o">:</span> <span class="n">Element</span> <span class="n">q</span> <span class="n">not</span> <span class="n">found</span>
</pre></div>
</div>
<p>This can cause your test to abort.  For some tests that’s what you want.  But
often that is not desirable as your test script has many other subsequent tests
to perform.</p>
<p>A better approach is to first validate whether the element is really present
and then take alternatives when it it is not.  Let’s look at this using Java.</p>
<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="c1">// If element is available on page then perform type operation.</span>
<span class="k">if</span><span class="o">(</span><span class="n">selenium</span><span class="o">.</span><span class="na">isElementPresent</span><span class="o">(</span><span class="s">"q"</span><span class="o">))</span> <span class="o">{</span>
    <span class="n">selenium</span><span class="o">.</span><span class="na">type</span><span class="o">(</span><span class="s">"q"</span><span class="o">,</span> <span class="s">"Selenium rc"</span><span class="o">);</span>
<span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
    <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">printf</span><span class="o">(</span><span class="s">"Element: "</span> <span class="o">+</span><span class="n">q</span><span class="o">+</span> <span class="s">" is not available on page."</span><span class="o">)</span>
<span class="o">}</span>
</pre></div>
</div>
<p>The advantage of this approach is to continue with test execution even if some UI
elements are not available on page.</p>
</div>
<div class="section" id="executing-javascript-from-your-test">
<h3>Executing JavaScript from Your Test<a class="headerlink" href="#executing-javascript-from-your-test" title="Permalink to this headline">¶</a></h3>
<p>JavaScript comes very handy in exercising an application which is not directly supported
by selenium. The <strong>getEval</strong> method of selenium API can be used to execute JavaScript from
selenium RC.</p>
<p>Consider an application having check boxes with no static identifiers.
In this case one could evaluate JavaScript from selenium RC to get ids of all
check boxes and then exercise them.</p>
<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="kd">public</span> <span class="kd">static</span> <span class="n">String</span><span class="o">[]</span> <span class="nf">getAllCheckboxIds</span> <span class="o">()</span> <span class="o">{</span>
             <span class="n">String</span> <span class="n">script</span> <span class="o">=</span> <span class="s">"var inputId  = new Array();"</span><span class="o">;</span><span class="c1">// Create array in java script.</span>
             <span class="n">script</span> <span class="o">+=</span> <span class="s">"var cnt = 0;"</span><span class="o">;</span> <span class="c1">// Counter for check box ids.</span>
             <span class="n">script</span> <span class="o">+=</span> <span class="s">"var inputFields  = new Array();"</span><span class="o">;</span> <span class="c1">// Create array in java script.</span>
             <span class="n">script</span> <span class="o">+=</span> <span class="s">"inputFields = window.document.getElementsByTagName('input');"</span><span class="o">;</span> <span class="c1">// Collect input elements.</span>
             <span class="n">script</span> <span class="o">+=</span> <span class="s">"for(var i=0; i&lt;inputFields.length; i++) {"</span><span class="o">;</span> <span class="c1">// Loop through the collected elements.</span>
             <span class="n">script</span> <span class="o">+=</span> <span class="s">"if(inputFields[i].id !=null "</span> <span class="o">+</span>
             <span class="s">"&amp;&amp; inputFields[i].id !='undefined' "</span> <span class="o">+</span>
             <span class="s">"&amp;&amp; inputFields[i].getAttribute('type') == 'checkbox') {"</span><span class="o">;</span> <span class="c1">// If input field is of type check box and input id is not null.</span>
             <span class="n">script</span> <span class="o">+=</span> <span class="s">"inputId[cnt]=inputFields[i].id ;"</span> <span class="o">+</span> <span class="c1">// Save check box id to inputId array.</span>
             <span class="s">"cnt++;"</span> <span class="o">+</span> <span class="c1">// increment the counter.</span>
             <span class="s">"}"</span> <span class="o">+</span> <span class="c1">// end of if.</span>
             <span class="s">"}"</span><span class="o">;</span> <span class="c1">// end of for.</span>
             <span class="n">script</span> <span class="o">+=</span> <span class="s">"inputId.toString();"</span> <span class="o">;</span><span class="c1">// Convert array in to string.</span>
             <span class="n">String</span><span class="o">[]</span> <span class="n">checkboxIds</span> <span class="o">=</span> <span class="n">selenium</span><span class="o">.</span><span class="na">getEval</span><span class="o">(</span><span class="n">script</span><span class="o">).</span><span class="na">split</span><span class="o">(</span><span class="s">","</span><span class="o">);</span> <span class="c1">// Split the string.</span>
             <span class="k">return</span> <span class="n">checkboxIds</span><span class="o">;</span>
 <span class="o">}</span>
</pre></div>
</div>
<p>To count number of images on a page:</p>
<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">selenium</span><span class="o">.</span><span class="na">getEval</span><span class="o">(</span><span class="s">"window.document.images.length;"</span><span class="o">);</span>
</pre></div>
</div>
<p>Remember to use window object in case of DOM expressions as by default selenium
window is referred to, not the test window.</p>
</div>
</div>
<div class="section" id="server-options">
<h2>Server Options<a class="headerlink" href="#server-options" title="Permalink to this headline">¶</a></h2>
<p>When the server is launched, command line options can be used to change the
default server behaviour.</p>
<p>Recall, the server is started by running the following.</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>$ java -jar selenium-server-standalone-&lt;version-number&gt;.jar
</pre></div>
</div>
<p>To see the list of options, run the server with the <code class="docutils literal"><span class="pre">-h</span></code> option.</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>$ java -jar selenium-server-standalone-&lt;version-number&gt;.jar -h
</pre></div>
</div>
<p>You’ll see a list of all the options you can use with the server and a brief
description of each. The provided descriptions will not always be enough, so we’ve
provided explanations for some of the more important options.</p>
<div class="section" id="proxy-configuration">
<h3>Proxy Configuration<a class="headerlink" href="#proxy-configuration" title="Permalink to this headline">¶</a></h3>
<p>If your AUT is behind an HTTP proxy which requires authentication then you should
configure http.proxyHost, http.proxyPort, http.proxyUser and http.proxyPassword
using the following command.</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>$ java -jar selenium-server-standalone-&lt;version-number&gt;.jar -Dhttp.proxyHost<span class="o">=</span>proxy.com -Dhttp.proxyPort<span class="o">=</span><span class="m">8080</span> -Dhttp.proxyUser<span class="o">=</span>username -Dhttp.proxyPassword<span class="o">=</span>password
</pre></div>
</div>
</div>
<div class="section" id="multi-window-mode">
<h3>Multi-Window Mode<a class="headerlink" href="#multi-window-mode" title="Permalink to this headline">¶</a></h3>
<p>If you are using Selenium 1.0 you can probably skip this section, since multiwindow mode is
the default behavior.  However, prior to version 1.0, Selenium by default ran the
application under test in a sub frame as shown here.</p>
<img alt="_images/chapt5_img26_single_window_mode.png" class="align-center" src="_images/chapt5_img26_single_window_mode.png">
<p>Some applications didn’t run correctly in a sub frame, and needed to be
loaded into the top frame of the window. The multi-window mode option allowed
the AUT to run in a separate window rather than in the default
frame where it could then have the top frame it required.</p>
<img alt="_images/chapt5_img27_multi_window_mode.png" class="align-center" src="_images/chapt5_img27_multi_window_mode.png">
<p>For older versions of Selenium you must specify multiwindow mode explicitly
with the following option:</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>-multiwindow
</pre></div>
</div>
<p>As of Selenium RC 1.0, if you want to run your test within a
single frame (i.e. using the standard for earlier Selenium versions)
you can state this to the Selenium Server using the option</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>-singlewindow
</pre></div>
</div>
</div>
<div class="section" id="specifying-the-firefox-profile">
<h3>Specifying the Firefox Profile<a class="headerlink" href="#specifying-the-firefox-profile" title="Permalink to this headline">¶</a></h3>
<p>Firefox will not run two instances simultaneously unless you specify a
separate profile for each instance. Selenium RC 1.0 and later runs in a
separate profile automatically, so if you are using Selenium 1.0, you can
probably skip this section.  However, if you’re using an older version of
Selenium or if you need to use a specific profile for your tests
(such as adding an https certificate or having some addons installed), you will
need to explicitly specify the profile.</p>
<p>First, to create a separate Firefox profile, follow this procedure.
Open the Windows Start menu, select “Run”, then type and enter one of the
following:</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>firefox.exe -profilemanager
</pre></div>
</div>
<div class="highlight-bash"><div class="highlight"><pre><span></span>firefox.exe -P
</pre></div>
</div>
<p>Create the new profile using the dialog. Then when you run Selenium Server,
tell it to use this new Firefox profile with the server command-line option
<em>-firefoxProfileTemplate</em> and specify the path to the profile using its filename
and directory path.</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>-firefoxProfileTemplate <span class="s2">"path to the profile"</span>
</pre></div>
</div>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">Be sure to put your profile in a new folder separate from the default!!!
The Firefox profile manager tool will delete all files in a folder if you
delete a profile, regardless of whether they are profile files or not.</p>
</div>
<p>More information about Firefox profiles can be found in <a class="reference external" href="http://support.mozilla.com/zh-CN/kb/Managing+profiles">Mozilla’s Knowledge Base</a></p>
</div>
<div class="section" id="run-selenese-directly-within-the-server-using-htmlsuite">
<span id="html-suite"></span><h3>Run Selenese Directly Within the Server Using -htmlSuite<a class="headerlink" href="#run-selenese-directly-within-the-server-using-htmlsuite" title="Permalink to this headline">¶</a></h3>
<p>You can run Selenese html files directly within the Selenium Server
by passing the html file to the server’s command line.  For instance:</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>java -jar selenium-server-standalone-&lt;version-number&gt;.jar -htmlSuite <span class="s2">"*firefox"</span>
<span class="s2">"http://www.google.com"</span> <span class="s2">"c:\absolute\path\to\my\HTMLSuite.html"</span>
<span class="s2">"c:\absolute\path\to\my\results.html"</span>
</pre></div>
</div>
<p>This will automatically launch your HTML suite, run all the tests and save a
nice HTML report with the results.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">When using this option, the server will start the tests and wait for a
specified number of seconds for the test to complete; if the test doesn’t
complete within that amount of time, the command will exit with a non-zero
exit code and no results file will be generated.</p>
</div>
<p>This command line is very long so be careful when
you type it. Note this requires you to pass in an HTML
Selenese suite, not a single test. Also be aware the -htmlSuite option is incompatible with <code class="docutils literal"><span class="pre">-interactive</span></code>
You cannot run both at the same time.</p>
</div>
<div class="section" id="selenium-server-logging">
<h3>Selenium Server Logging<a class="headerlink" href="#selenium-server-logging" title="Permalink to this headline">¶</a></h3>
<div class="section" id="server-side-logs">
<h4>Server-Side Logs<a class="headerlink" href="#server-side-logs" title="Permalink to this headline">¶</a></h4>
<p>When launching selenium server the <strong>-log</strong> option can be used to record
valuable debugging information reported by the Selenium Server to a text file.</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>java -jar selenium-server-standalone-&lt;version-number&gt;.jar -log selenium.log
</pre></div>
</div>
<p>This log file is more verbose than the standard console logs (it includes DEBUG
level logging messages). The log file also includes the logger name, and the ID
number of the thread that logged the message. For example:</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span><span class="m">20</span>:44:25 DEBUG <span class="o">[</span><span class="m">12</span><span class="o">]</span> org.openqa.selenium.server.SeleniumDriverResourceHandler -
Browser <span class="m">465828</span>/:top frame1 posted START NEW
</pre></div>
</div>
<p>The message format is</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>TIMESTAMP<span class="o">(</span>HH:mm:ss<span class="o">)</span> LEVEL <span class="o">[</span>THREAD<span class="o">]</span> LOGGER - MESSAGE
</pre></div>
</div>
<p>This message may be multiline.</p>
</div>
<div class="section" id="browser-side-logs">
<h4>Browser-Side Logs<a class="headerlink" href="#browser-side-logs" title="Permalink to this headline">¶</a></h4>
<p>JavaScript on the browser side (Selenium Core) also logs important messages;
in many cases, these can be more useful to the end-user than the regular Selenium
Server logs. To access browser-side logs, pass the <strong>-browserSideLog</strong>
argument to the Selenium Server.</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>java -jar selenium-server-standalone-&lt;version-number&gt;.jar -browserSideLog
</pre></div>
</div>
<p><strong>-browserSideLog</strong> must be combined with the <strong>-log</strong> argument, to log
browserSideLogs (as well as all other DEBUG level logging messages) to a file.</p>
</div>
</div>
</div>
<div class="section" id="specifying-the-path-to-a-specific-browser">
<h2>Specifying the Path to a Specific Browser<a class="headerlink" href="#specifying-the-path-to-a-specific-browser" title="Permalink to this headline">¶</a></h2>
<p>You can specify to Selenium RC a path to a specific browser. This is useful if
you have different versions of the same browser and you wish to use a specific
one. Also, this is used to allow your tests to run against a browser not
directly supported by Selenium RC. When specifying the run mode, use the
*custom specifier followed by the full path to the browser’s executable:</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>*custom &lt;path to browser&gt;
</pre></div>
</div>
</div>
<div class="section" id="selenium-rc-architecture">
<h2>Selenium RC Architecture<a class="headerlink" href="#selenium-rc-architecture" title="Permalink to this headline">¶</a></h2>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">This topic tries to explain the technical implementation behind
Selenium RC. It’s not fundamental for a Selenium user to know this, but
could be useful for understanding some of the problems you might find in the
future.</p>
</div>
<p>To understand in detail how Selenium RC Server works  and why it uses proxy injection
and heightened privilege modes you must first understand <a class="reference internal" href="#the-same-origin-policy">the same origin policy</a>.</p>
<div class="section" id="the-same-origin-policy">
<h3>The Same Origin Policy<a class="headerlink" href="#the-same-origin-policy" title="Permalink to this headline">¶</a></h3>
<p>The main restriction that Selenium faces is the
Same Origin Policy. This security restriction is applied by every browser
in the market and its objective is to ensure that a site’s content will never
be accessible by a script from another site.  The Same Origin Policy dictates that
any code loaded within the browser can only operate within that website’s domain.
It cannot perform functions on another website.  So for example, if the browser
loads JavaScript code when it loads www.mysite.com, it cannot run that loaded code
against www.mysite2.com–even if that’s another of your sites. If this were possible,
a script placed on any website you open would be able to read information on
your bank account if you had the account page
opened on other tab. This is called XSS (Cross-site Scripting).</p>
<p>To work within this policy, Selenium-Core (and its JavaScript commands that
make all the magic happen) must be placed in the same origin as the Application
Under Test (same URL).</p>
<p>Historically, Selenium-Core was limited by this problem since it was implemented in
JavaScript.  Selenium RC is not, however, restricted by the Same Origin Policy.  Its
use of the Selenium Server as a proxy avoids this problem.  It, essentially, tells the
browser that the browser is working on a single “spoofed” website that the Server
provides.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">You can find additional information about this topic on Wikipedia
pages about <a class="reference external" href="http://en.wikipedia.org/wiki/Same_origin_policy">Same Origin Policy</a> and <a class="reference external" href="http://en.wikipedia.org/wiki/Cross-site_scripting">XSS</a>.</p>
</div>
</div>
<div class="section" id="proxy-injection">
<h3>Proxy Injection<a class="headerlink" href="#proxy-injection" title="Permalink to this headline">¶</a></h3>
<p>The first method Selenium used to avoid the <a class="reference internal" href="#the-same-origin-policy">The Same Origin Policy</a> was Proxy Injection.
In Proxy Injection Mode, the Selenium Server acts as a client-configured <a class="footnote-reference" href="#id3" id="id1">[1]</a> <strong>HTTP
proxy</strong> <a class="footnote-reference" href="#id4" id="id2">[2]</a>, that sits between the browser and the Application Under Test.
It then masks the AUT under a fictional URL (embedding
Selenium-Core and the set of tests and delivering them as if they were coming
from the same origin).</p>
<table class="docutils footnote" frame="void" id="id3" rules="none">
<colgroup><col class="label"><col></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id1">[1]</a></td><td>The proxy is a third person in the middle that passes the ball
between the two parts. It acts as a “web server” that
delivers the AUT to the browser. Being a proxy gives Selenium
Server the capability of “lying” about the AUT’s real URL.</td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id4" rules="none">
<colgroup><col class="label"><col></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id2">[2]</a></td><td>The browser is launched with a
configuration profile that has set localhost:4444 as the HTTP proxy, this
is why any HTTP request that the browser does will pass through Selenium
server and the response will pass through it and not from the real server.</td></tr>
</tbody>
</table>
<p>Here is an architectural diagram.</p>
<img alt="_images/chapt5_img02_Architecture_Diagram_1.png" class="align-center" src="_images/chapt5_img02_Architecture_Diagram_1.png">
<p>As a test suite starts in your favorite language, the following happens:</p>
<ol class="arabic simple">
<li>The client/driver establishes a connection with the selenium-RC server.</li>
<li>Selenium RC server launches a browser (or reuses an old one) with a URL
that injects Selenium-Core’s JavaScript into the browser-loaded web page.</li>
<li>The client-driver passes a Selenese command to the server.</li>
<li>The Server interprets the command and then triggers the corresponding
JavaScript execution to execute that command within the browser.
Selenium-Core instructs the browser to act on that first instruction, typically opening a page of the
AUT.</li>
<li>The browser receives the open request and asks for the website’s content from
the Selenium RC server (set as the HTTP proxy for the browser to use).</li>
<li>Selenium RC server communicates with the Web server asking for the page and once
it receives it, it sends the page to the browser masking the origin to look
like the page comes from the same server as Selenium-Core (this allows
Selenium-Core to comply with the Same Origin Policy).</li>
<li>The browser receives the web page and renders it in the frame/window reserved
for it.</li>
</ol>
</div>
<div class="section" id="heightened-privileges-browsers">
<h3>Heightened Privileges Browsers<a class="headerlink" href="#heightened-privileges-browsers" title="Permalink to this headline">¶</a></h3>
<p>This workflow in this method is very similar to Proxy Injection but the main
difference is that the browsers are launched in a special mode called <em>Heightened
Privileges</em>, which allows websites to do things that are not commonly permitted
(as doing <a class="reference external" href="http://en.wikipedia.org/wiki/Cross-site_scripting">XSS</a>, or filling file upload inputs and pretty useful stuff for
Selenium). By using these browser modes, Selenium Core is able to directly open
the AUT and read/interact with its content without having to pass the whole AUT
through the Selenium RC server.</p>
<p>Here is the architectural diagram.</p>
<img alt="_images/chapt5_img02_Architecture_Diagram_2.png" class="align-center" src="_images/chapt5_img02_Architecture_Diagram_2.png">
<p>As a test suite starts in your favorite language, the following happens:</p>
<ol class="arabic simple">
<li>The client/driver establishes a connection with the selenium-RC server.</li>
<li>Selenium RC server launches a browser (or reuses an old one) with a URL
that will load Selenium-Core in the web page.</li>
<li>Selenium-Core gets the first instruction from the client/driver (via another
HTTP request made to the Selenium RC Server).</li>
<li>Selenium-Core acts on that first instruction, typically opening a page of the
AUT.</li>
<li>The browser receives the open request and asks the Web Server for the page.
Once the browser receives the web page, renders it in the frame/window reserved
for it.</li>
</ol>
</div>
</div>
<div class="section" id="handling-https-and-security-popups">
<h2>Handling HTTPS and Security Popups<a class="headerlink" href="#handling-https-and-security-popups" title="Permalink to this headline">¶</a></h2>
<p>Many applications switch from using HTTP to HTTPS when they need to send
encrypted information such as passwords or credit card information. This is
common with many of today’s web applications. Selenium RC supports this.</p>
<p>To ensure the HTTPS site is genuine, the browser will need a security
certificate. Otherwise, when the browser accesses the AUT using HTTPS, it will
assume that application is not ‘trusted’. When this occurs the browser
displays security popups, and these popups cannot be closed using Selenium RC.</p>
<p>When dealing with HTTPS in a Selenium RC test, you must use a run mode that supports this and handles
the security certificate for you. You specify the run mode when your test program
initializes Selenium.</p>
<p>In Selenium RC 1.0 beta 2 and later use *firefox or *iexplore for the run
mode. In earlier versions, including Selenium RC 1.0 beta 1, use *chrome or
*iehta, for the run mode. Using these run modes, you will not need to install
any special security certificates; Selenium RC will handle it for you.</p>
<p>In version 1.0 the run modes *firefox or *iexplore are
recommended. However, there are additional run modes of *iexploreproxy and
*firefoxproxy. These are provided for backwards compatibility only, and
should not be used unless required by legacy test programs. Their use will
present limitations with security certificate handling and with the running
of multiple windows if your application opens additional browser windows.</p>
<p>In earlier versions of Selenium RC, *chrome or *iehta were the run modes that
supported HTTPS and the handling of security popups. These were considered ‘experimental
modes although they became quite stable and many people used them.  If you are using
Selenium 1.0 you do not need, and should not use, these older run modes.</p>
<div class="section" id="security-certificates-explained">
<h3>Security Certificates Explained<a class="headerlink" href="#security-certificates-explained" title="Permalink to this headline">¶</a></h3>
<p>Normally, your browser will trust the application you are testing
by installing a security certificate which you already own. You can
check this in your browser’s options or Internet properties (if you don’t
know your AUT’s security certificate ask your system administrator).
When Selenium loads your browser it injects code to intercept
messages between the browser and the server. The browser now thinks
untrusted software is trying to look like your application.  It responds by alerting you with popup messages.</p>
<p>To get around this, Selenium RC, (again when using a run mode that support
this) will install its own security certificate, temporarily, to your
client machine in a place where the browser can access it. This tricks the
browser into thinking it’s accessing a site different from your AUT and effectively suppresses the popups.</p>
<p>Another method used with earlier versions of Selenium was to
install the Cybervillians security certificate provided with your Selenium
installation. Most users should no longer need to do this however; if you are
running Selenium RC in proxy injection mode, you may need to explicitly install this
security certificate.</p>
</div>
</div>
<div class="section" id="supporting-additional-browsers-and-browser-configurations">
<h2>Supporting Additional Browsers and Browser Configurations<a class="headerlink" href="#supporting-additional-browsers-and-browser-configurations" title="Permalink to this headline">¶</a></h2>
<p>The Selenium API supports running against multiple browsers in addition to
Internet Explorer and Mozilla Firefox.  See the SeleniumHQ.org website for
supported browsers.  In addition, when a browser is not directly supported,
you may still run your Selenium tests against a browser of your choosing by
using the “*custom” run-mode (i.e. in place of *firefox or *iexplore) when
your test application starts the browser.  With this, you pass in the path to
the browsers executable within the API call. This can also be done from the
Server in interactive mode.</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span><span class="nv">cmd</span><span class="o">=</span>getNewBrowserSession<span class="p">&amp;</span><span class="nv">1</span><span class="o">=</span>*custom c:<span class="se">\P</span>rogram Files<span class="se">\M</span>ozilla Firefox<span class="se">\M</span>yBrowser.exe<span class="p">&amp;</span><span class="nv">2</span><span class="o">=</span>http://www.google.com
</pre></div>
</div>
<div class="section" id="running-tests-with-different-browser-configurations">
<h3>Running Tests with Different Browser Configurations<a class="headerlink" href="#running-tests-with-different-browser-configurations" title="Permalink to this headline">¶</a></h3>
<p>Normally Selenium RC automatically configures the browser, but if you launch
the browser using the “*custom” run mode, you can force Selenium RC
to launch the browser as-is, without using an automatic configuration.</p>
<p>For example, you can launch Firefox with a custom configuration like this:</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span><span class="nv">cmd</span><span class="o">=</span>getNewBrowserSession<span class="p">&amp;</span><span class="nv">1</span><span class="o">=</span>*custom c:<span class="se">\P</span>rogram Files<span class="se">\M</span>ozilla Firefox<span class="se">\f</span>irefox.exe<span class="p">&amp;</span><span class="nv">2</span><span class="o">=</span>http://www.google.com
</pre></div>
</div>
<p>Note that when launching the browser this way, you must manually
configure the browser to use the Selenium Server as a proxy. Normally this just
means opening your browser preferences and specifying “localhost:4444” as
an HTTP proxy, but instructions for this can differ radically from browser to
browser.  Consult your browser’s documentation for details.</p>
<p>Be aware that Mozilla browsers can vary in how they start and stop.
One may need to set the MOZ_NO_REMOTE environment variable to make Mozilla browsers
behave a little more predictably. Unix users should avoid launching the browser using
a shell script; it’s generally better to use the binary executable (e.g. firefox-bin) directly.</p>
</div>
</div>
<div class="section" id="troubleshooting-common-problems">
<h2>Troubleshooting Common Problems<a class="headerlink" href="#troubleshooting-common-problems" title="Permalink to this headline">¶</a></h2>
<p>When getting started with Selenium RC there’s a few potential problems
that are commonly encountered.  We present them along with their solutions here.</p>
<div class="section" id="unable-to-connect-to-server">
<h3>Unable to Connect to Server<a class="headerlink" href="#unable-to-connect-to-server" title="Permalink to this headline">¶</a></h3>
<p>When your test program cannot connect to the Selenium Server, Selenium throws an exception in your test program.
It should display this message or a similar one:</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span><span class="s2">"Unable to connect to remote server (Inner Exception Message:</span>
<span class="s2">    No connection could be made because the target machine actively</span>
<span class="s2">    refused it )"</span>

    <span class="o">(</span>using .NET and XP Service Pack <span class="m">2</span><span class="o">)</span>
</pre></div>
</div>
<p>If you see a message like this, be sure you started the Selenium Server. If
so, then there is a problem with the connectivity between the Selenium Client
Library and the Selenium Server.</p>
<p>When starting with Selenium RC, most people begin by running their test program
(with a Selenium Client Library) and the Selenium Server on the same machine.  To
do this use “localhost” as your connection parameter.
We recommend beginning this way since it reduces the influence of potential networking problems
which you’re getting started.  Assuming your operating system has typical networking
and TCP/IP settings you should have little difficulty.  In truth, many people
choose to run the tests this way.</p>
<p>If, however, you do want to run Selenium Server
on a remote machine, the connectivity should be fine assuming you have valid TCP/IP
connectivity between the two machines.</p>
<p>If you have difficulty connecting, you can use common networking tools like <em>ping</em>,
<em>telnet</em>, <em>ifconfig(Unix)/ipconfig</em> (Windows), etc to ensure you have a valid
network connection.  If unfamilar with these, your system administrator can assist you.</p>
</div>
<div class="section" id="unable-to-load-the-browser">
<h3>Unable to Load the Browser<a class="headerlink" href="#unable-to-load-the-browser" title="Permalink to this headline">¶</a></h3>
<p>Ok, not a friendly error message, sorry, but if the Selenium Server cannot load the browser
you will likely see this error.</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span><span class="o">(</span><span class="m">500</span><span class="o">)</span> Internal Server Error
</pre></div>
</div>
<p>This could be caused by</p>
<ul class="simple">
<li>Firefox (prior to Selenium 1.0) cannot start because the browser is already open and you did
not specify a separate profile.   See the section on Firefox profiles under Server Options.</li>
<li>The run mode you’re using doesn’t match any browser on your machine.  Check the parameters you
passed to Selenium when you program opens the browser.</li>
<li>You specified the path to the browser explicitly (using “*custom”–see above) but the path is
incorrect.  Check to be sure the path is correct.  Also check the user group to be sure there are
no known issues with your browser and the “*custom” parameters.</li>
</ul>
</div>
<div class="section" id="selenium-cannot-find-the-aut">
<h3>Selenium Cannot Find the AUT<a class="headerlink" href="#selenium-cannot-find-the-aut" title="Permalink to this headline">¶</a></h3>
<p>If your test program starts the browser successfully, but the browser doesn’t
display the website you’re testing, the most likely cause is your test
program is not using the correct URL.</p>
<p>This can easily happen. When you use Selenium-IDE to export your script,
it inserts a dummy URL. You must manually change the URL to the correct one
for your application to be tested.</p>
</div>
<div class="section" id="firefox-refused-shutdown-while-preparing-a-profile">
<h3>Firefox Refused Shutdown While Preparing a Profile<a class="headerlink" href="#firefox-refused-shutdown-while-preparing-a-profile" title="Permalink to this headline">¶</a></h3>
<p>This most often occurs when you run your Selenium RC test program against Firefox,
but you already have a Firefox browser session running and, you didn’t specify
a separate profile when you started the Selenium Server. The error from the
test program looks like this:</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>Error:  java.lang.RuntimeException: Firefox refused shutdown <span class="k">while</span>
preparing a profile
</pre></div>
</div>
<p>Here’s the complete error message from the server:</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span><span class="m">16</span>:20:03.919 INFO - Preparing Firefox profile...
<span class="m">16</span>:20:27.822 WARN - GET /selenium-server/driver/?cmd<span class="o">=</span>getNewBrowserSession<span class="p">&amp;</span><span class="nv">1</span><span class="o">=</span>*fir
efox<span class="p">&amp;</span><span class="nv">2</span><span class="o">=</span>http%3a%2f%2fsage-webapp1.qa.idc.com HTTP/1.1
java.lang.RuntimeException: Firefox refused shutdown <span class="k">while</span> preparing a profile
        at org.openqa.selenium.server.browserlaunchers.FirefoxCustomProfileLaunc
her.waitForFullProfileToBeCreated<span class="o">(</span>FirefoxCustomProfileLauncher.java:277<span class="o">)</span>
...
Caused by: org.openqa.selenium.server.browserlaunchers.FirefoxCustomProfileLaunc
her<span class="nv">$FileLockRemainedException</span>: Lock file still present! C:<span class="se">\D</span>OCUME~1<span class="se">\j</span>svec<span class="se">\L</span>OCALS
~1<span class="se">\T</span>emp<span class="se">\c</span>ustomProfileDir203138<span class="se">\p</span>arent.lock
</pre></div>
</div>
<p>To resolve this, see the section on <a class="reference internal" href="#specifying-the-firefox-profile">Specifying a Separate Firefox Profile</a></p>
</div>
<div class="section" id="versioning-problems">
<h3>Versioning Problems<a class="headerlink" href="#versioning-problems" title="Permalink to this headline">¶</a></h3>
<p>Make sure your version of Selenium supports the version of your browser. For
example, Selenium RC 0.92 does not support Firefox 3. At times you may be lucky
(I was). But don’t forget to check which
browser versions are supported by the version of Selenium you are using. When in
doubt, use the latest release version of Selenium with the most widely used version
of your browser.</p>
</div>
<div class="section" id="error-message-unsupported-major-minor-version-49-0-while-starting-server">
<h3>Error message: “(Unsupported major.minor version 49.0)” while starting server<a class="headerlink" href="#error-message-unsupported-major-minor-version-49-0-while-starting-server" title="Permalink to this headline">¶</a></h3>
<p>This error says you’re not using a correct version of Java.
The Selenium Server requires Java 1.5 or higher.</p>
<p>To check double-check your java version, run this from the command line.</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>java -version
</pre></div>
</div>
<p>You should see a message showing the Java version.</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>java version <span class="s2">"1.5.0_07"</span>
Java<span class="o">(</span>TM<span class="o">)</span> <span class="m">2</span> Runtime Environment, Standard Edition <span class="o">(</span>build <span class="m">1</span>.5.0_07-b03<span class="o">)</span>
Java HotSpot<span class="o">(</span>TM<span class="o">)</span> Client VM <span class="o">(</span>build <span class="m">1</span>.5.0_07-b03, mixed mode<span class="o">)</span>
</pre></div>
</div>
<p>If you see a lower version number, you may need to update the JRE,
or you may simply need to add it to your PATH environment variable.</p>
</div>
<div class="section" id="error-when-running-the-getnewbrowsersession-command">
<h3>404 error when running the getNewBrowserSession command<a class="headerlink" href="#error-when-running-the-getnewbrowsersession-command" title="Permalink to this headline">¶</a></h3>
<p>If you’re getting a 404 error while attempting to open a page on
“<a class="reference external" href="http://www.google.com/selenium-server/">http://www.google.com/selenium-server/</a>”, then it must be because the Selenium
Server was not correctly configured as a proxy. The “selenium-server” directory
doesn’t exist on google.com; it only appears to exist when the proxy is
properly configured. Proxy Configuration highly depends on how the browser is
launched with *firefox, *iexplore, *opera, or *custom.</p>
<blockquote>
<div><blockquote>
<div><ul class="simple">
<li>*iexplore: If the browser is launched using *iexplore, you could be
having a problem with Internet Explorer’s proxy settings.  Selenium
Server attempts To configure the global proxy settings in the Internet
Options Control Panel. You must make sure that those are correctly
configured when Selenium Server launches the browser. Try looking at
your Internet Options control panel. Click on the “Connections” tab
and click on “LAN Settings”.<ul>
<li>If you need to use a proxy to access the application you want to test,
you’ll need to start Selenium Server with “-Dhttp.proxyHost”;
see the <a class="reference internal" href="#proxy-configuration">Proxy Configuration</a> for more details.</li>
<li>You may also try configuring your proxy manually and then launching
the browser with *custom, or with *iehta browser launcher.</li>
</ul>
</li>
</ul>
</div></blockquote>
<ul class="simple">
<li><dl class="first docutils">
<dt>*custom: When using *custom you must configure the proxy correctly(manually),</dt>
<dd>otherwise you’ll get a 404 error. Double-check that you’ve configured your proxy
settings correctly. To check whether you’ve configured the proxy correctly is to
attempt to intentionally configure the browser incorrectly. Try configuring the
browser to use the wrong proxy server hostname, or the wrong port.  If you had
successfully configured the browser’s proxy settings incorrectly, then the
browser will be unable to connect to the Internet, which is one way to make
sure that one is adjusting the relevant settings.</dd>
</dl>
</li>
<li>For other browsers (*firefox, *opera) we automatically hard-code
the proxy for you, and so there are no known issues with this functionality.
If you’re encountering 404 errors and have followed this user guide carefully
post your results to user group for some help from the user community.</li>
</ul>
</div></blockquote>
</div>
<div class="section" id="permission-denied-error">
<h3>Permission Denied Error<a class="headerlink" href="#permission-denied-error" title="Permalink to this headline">¶</a></h3>
<p>The most common reason for this error is that your session is attempting to violate
the same-origin policy by crossing domain boundaries (e.g., accesses a page from
<a class="reference external" href="http://domain1">http://domain1</a> and then accesses a page from <a class="reference external" href="http://domain2">http://domain2</a>) or switching protocols
(moving from <a class="reference external" href="http://domainX">http://domainX</a> to <a class="reference external" href="https://domainX">https://domainX</a>).</p>
<p>This error can also occur when JavaScript attempts to find UI objects
which are not yet available (before the page has completely loaded), or
are no longer available (after the page has started
to be unloaded). This is most typically encountered with AJAX pages
which are working with sections of a page or subframes that load and/or reload
independently of the larger page.</p>
<p>This error can be intermittent. Often it is impossible to reproduce the problem
with a debugger because the trouble stems from race conditions which
are not reproducible when the debugger’s overhead is added to the system.
Permission issues are covered in some detail in the tutorial. Read the section
about the <a class="reference internal" href="#the-same-origin-policy">The Same Origin Policy</a>, <a class="reference internal" href="#proxy-injection">Proxy Injection</a> carefully.</p>
</div>
<div class="section" id="handling-browser-popup-windows">
<h3>Handling Browser Popup Windows<a class="headerlink" href="#handling-browser-popup-windows" title="Permalink to this headline">¶</a></h3>
<p>There are several kinds of “Popups” that you can get during a Selenium test.
You may not be able to close these popups by running selenium commands if
they are initiated by the browser and not your AUT.  You may
need to know how to manage these.  Each type of popup needs to be addressed differently.</p>
<blockquote>
<div><ul class="simple">
<li>HTTP basic authentication dialogs: These dialogs prompt for a
username/password to login to the site. To login to a site that requires
HTTP basic authentication, use a username and password in the URL, as
described in <a class="reference external" href="http://tools.ietf.org/html/rfc1738#section-3.1">RFC 1738</a>, like this: open(“<a class="reference external" href="http://myusername:myuserpassword@myexample.com/blah/blah/blah">http://myusername:myuserpassword@myexample.com/blah/blah/blah</a>”).</li>
<li>SSL certificate warnings: Selenium RC automatically attempts to spoof SSL
certificates when it is enabled as a proxy; see more on this
in the section on HTTPS. If your browser is configured correctly,
you should never see SSL certificate warnings, but you may need to
configure your browser to trust our dangerous “CyberVillains” SSL certificate
authority. Again, refer to the HTTPS section for how to do this.</li>
<li>modal JavaScript alert/confirmation/prompt dialogs: Selenium tries to conceal
those dialogs from you (by replacing window.alert, window.confirm and
window.prompt) so they won’t stop the execution of your page. If you’re
seeing an alert pop-up, it’s probably because it fired during the page load process,
which is usually too early for us to protect the page.  Selenese contains commands
for asserting or verifying alert and confirmation popups. See the sections on these
topics in Chapter 4.</li>
</ul>
</div></blockquote>
</div>
<div class="section" id="on-linux-why-isn-t-my-firefox-browser-session-closing">
<h3>On Linux, why isn’t my Firefox browser session closing?<a class="headerlink" href="#on-linux-why-isn-t-my-firefox-browser-session-closing" title="Permalink to this headline">¶</a></h3>
<p>On Unix/Linux you must invoke “firefox-bin” directly, so make sure that
executable is on the path. If executing Firefox through a
shell script, when it comes time to kill the browser Selenium RC will kill
the shell script, leaving the browser running.   You can specify the path
to firefox-bin directly, like this.</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span><span class="nv">cmd</span><span class="o">=</span>getNewBrowserSession<span class="p">&amp;</span><span class="nv">1</span><span class="o">=</span>*firefox /usr/local/firefox/firefox-bin<span class="p">&amp;</span><span class="nv">2</span><span class="o">=</span>http://www.google.com
</pre></div>
</div>
</div>
<div class="section" id="firefox-chrome-doesn-t-work-with-custom-profile">
<h3>Firefox *chrome doesn’t work with custom profile<a class="headerlink" href="#firefox-chrome-doesn-t-work-with-custom-profile" title="Permalink to this headline">¶</a></h3>
<p>Check Firefox profile folder -&gt; prefs.js -&gt; user_pref(“browser.startup.page”, 0);
Comment this line like this: “//user_pref(“browser.startup.page”, 0);” and try again.</p>
</div>
<div class="section" id="is-it-ok-to-load-a-custom-pop-up-as-the-parent-page-is-loading-i-e-before-the-parent-page-s-javascript-window-onload-function-runs">
<h3>Is it ok to load a custom pop-up as the parent page is loading (i.e., before the parent page’s javascript window.onload() function runs)?<a class="headerlink" href="#is-it-ok-to-load-a-custom-pop-up-as-the-parent-page-is-loading-i-e-before-the-parent-page-s-javascript-window-onload-function-runs" title="Permalink to this headline">¶</a></h3>
<p>No. Selenium relies on interceptors to determine window names as they are being loaded.
These interceptors work best in catching new windows if the windows are loaded AFTER
the onload() function. Selenium may not recognize windows loaded before the onload function.</p>
<blockquote>
<div></div></blockquote>
</div>
<div class="section" id="problems-with-verify-commands">
<h3>Problems With Verify Commands<a class="headerlink" href="#problems-with-verify-commands" title="Permalink to this headline">¶</a></h3>
<p>If you export your tests from Selenium-IDE, you may find yourself getting
empty verify strings from your tests (depending on the programming language
used).</p>
<p><em>Note: This section is not yet developed.</em></p>
</div>
<div class="section" id="safari-and-multiwindow-mode">
<h3>Safari and MultiWindow Mode<a class="headerlink" href="#safari-and-multiwindow-mode" title="Permalink to this headline">¶</a></h3>
<p><em>Note: This section is not yet developed.</em></p>
</div>
<div class="section" id="firefox-on-linux">
<h3>Firefox on Linux<a class="headerlink" href="#firefox-on-linux" title="Permalink to this headline">¶</a></h3>
<p>On Unix/Linux, versions of Selenium before 1.0 needed to invoke “firefox-bin”
directly, so if you are using a previous version, make sure that the real
executable is on the path.</p>
<p>On most Linux distributions, the real <em>firefox-bin</em> is located on:</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>/usr/lib/firefox-x.x.x/
</pre></div>
</div>
<p>Where the x.x.x is the version number you currently have. So, to add that path
to the user’s path. you will have to add the following to your .bashrc file:</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span><span class="nb">export</span> <span class="nv">PATH</span><span class="o">=</span><span class="s2">"</span><span class="nv">$PATH</span><span class="s2">:/usr/lib/firefox-x.x.x/"</span>
</pre></div>
</div>
<p>If necessary, you can specify the path to firefox-bin directly in your test,
like this:</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span><span class="s2">"*firefox /usr/lib/firefox-x.x.x/firefox-bin"</span>
</pre></div>
</div>
</div>
<div class="section" id="ie-and-style-attributes">
<h3>IE and Style Attributes<a class="headerlink" href="#ie-and-style-attributes" title="Permalink to this headline">¶</a></h3>
<p>If you are running your tests on Internet Explorer and you cannot locate
elements using their <cite>style</cite> attribute.
For example:</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>//td<span class="o">[</span>@style<span class="o">=</span><span class="s2">"background-color:yellow"</span><span class="o">]</span>
</pre></div>
</div>
<p>This would work perfectly in Firefox, Opera or Safari but not with IE.
IE interprets the keys in  <cite>@style</cite> as uppercase. So, even if the
source code is in lowercase, you should use:</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>//td<span class="o">[</span>@style<span class="o">=</span><span class="s2">"BACKGROUND-COLOR:yellow"</span><span class="o">]</span>
</pre></div>
</div>
<p>This is a problem if your test is intended to work on multiple browsers, but
you can easily code your test to detect the situation and try the alternative
locator that only works in IE.</p>
</div>
<div class="section" id="error-encountered-cannot-convert-object-to-primitive-value-with-shut-down-of-googlechrome-browser">
<h3>Error encountered - “Cannot convert object to primitive value” with shut down of  *googlechrome  browser<a class="headerlink" href="#error-encountered-cannot-convert-object-to-primitive-value-with-shut-down-of-googlechrome-browser" title="Permalink to this headline">¶</a></h3>
<p>To avoid this error you have to start browser with an option that disables same origin policy checks:</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>selenium.start<span class="o">(</span><span class="s2">"commandLineFlags=--disable-web-security"</span><span class="o">)</span><span class="p">;</span>
</pre></div>
</div>
</div>
<div class="section" id="error-encountered-in-ie-couldn-t-open-app-window-is-the-pop-up-blocker-enabled">
<h3>Error encountered in IE - “Couldn’t open app window; is the pop-up blocker enabled?”<a class="headerlink" href="#error-encountered-in-ie-couldn-t-open-app-window-is-the-pop-up-blocker-enabled" title="Permalink to this headline">¶</a></h3>
<p>To avoid this error you have to configure the browser: disable the popup blocker AND uncheck ‘Enable Protected Mode’ option in Tools &gt;&gt; Options &gt;&gt; Security.</p>
</div>
<div class="section" id="where-can-i-ask-questions-that-aren-t-answered-here">
<h3>Where can I Ask Questions that Aren’t Answered Here?<a class="headerlink" href="#where-can-i-ask-questions-that-aren-t-answered-here" title="Permalink to this headline">¶</a></h3>
<p>Try our <a class="reference external" href="http://seleniumhq.org/support/">user group</a></p>
</div>
</div>
</div>


          </div>

</body>
</html>